Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- dfs
- BFS
- java
- 프로그래머스
- 동적계획법
- 스택
- 백트래킹
- 후니의 쉽게 쓴 시스코 네트워킹
- 세그먼트 트리
- Kotlin
- Network
- 알고리즘
- 이분탐색
- Effective Java
- 에라토스테네스의 체
- 문자열
- 위상정렬
- 완전탐색
- 구현
- 투 포인터
- 수학
- 그리디
- 시뮬레이션
- mst
- JUnit 5
- 유니온 파인드
- CS
- 플로이드-와샬
- 백준
- swea
Archives
반갑습니다!
[백준] 15971 두 로봇 본문
풀이
두 로봇을 양 시작점과 끝점이라고 생각하면, 시작점부터 끝점까지의 길이의 합에서 가장 긴 길이를 빼면 된다.
코드
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool finish = false;
int n, r1, r2;
vector<pair<int, int>> adj[100001];
bool visited[100001];
void dfs(int idx, int sum, int max_value){
// 답을 찾았으면 탐색을 종료
if (finish) return;
// 경로를 다 찾은 경우
if (idx == r2) {
finish = true;
cout << sum - max_value << '\n';
return;
}
visited[idx] = true;
for (auto i : adj[idx]) {
int next = i.first;
int value = i.second;
if (!visited[next])
dfs(next, sum + value, max(max_value, value));
}
}
int main() {
cin >> n >> r1 >> r2;
for (int i = 0; i < n - 1; i++) {
int tmp1, tmp2, v;
cin >> tmp1 >> tmp2 >> v;
adj[tmp1].push_back({ tmp2, v });
adj[tmp2].push_back({ tmp1, v });
}
dfs(r1, 0, 0);
return 0;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[SWEA] Ladder1 (0) | 2020.04.26 |
---|---|
[SWEA] 자기 방으로 돌아가기 (0) | 2020.04.25 |
[프로그래머스] 구명보트 (0) | 2020.04.24 |
[프로그래머스] n진수 게임 (0) | 2020.04.24 |
[백준] 5566 주사위 게임 (0) | 2020.04.24 |