-
두 로봇 - 15971Algorithm/BOJ 2020. 3. 15. 21:5112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970//15971 - 두 로봇#include <iostream>#include <vector>#include <queue>using namespace std;#define INF 1000000000int N, S, E;vector<vector<pair<int, int>>> adj;void init(){cin >> N >> S >> E;adj = vector<vector<pair<int, int>>>(N+1);int x, y, r;for(int i = 0; i<N-1; ++i){cin >> x >> y >> r;adj[x].push_back(make_pair(y, r));adj[y].push_back(make_pair(x, r));}}void dijkstra(){priority_queue<pair<int, int>> pq;vector<int> dist(N+1, INF);vector<int> parent(N+1);vector<int> length(N+1);pq.push(make_pair(0, S));dist[S] = 0;parent[S] = S;while(!pq.empty()){int here = pq.top().second;int cost = -pq.top().first;pq.pop();if(dist[here] < cost) continue;for(int i = 0; i<adj[here].size(); ++i){int there = adj[here][i].first;int c = adj[here][i].second;if(cost + c < dist[there]){dist[there] = cost + c;parent[there] = here;length[there] = c;pq.push(make_pair(-c-cost, there));}}}int u = 0;for(int i = E; i != parent[i]; i = parent[i])u = max(u, length[i]);cout << dist[E] - u;}int main(){ios_base::sync_with_stdio(0); cin.tie(0);init();dijkstra();}
cs https://www.acmicpc.net/problem/15971
15971번: 두 로봇
입력에서 두 번째 줄에 주어지는 방번호는 1과 2, 세 번째 줄에 주어지는 방 번호는 2와 3, …, i번째 줄에 주어지는 방 번호는 i-1과 i, …, N번째 줄에 주어지는 방 번호는 N-1과 N이다 (아래 입력과 출력의 예에서 예제 입력 1을 참고).
www.acmicpc.net
두 로봇
두 지점 사이에 최단거리를 구하고, 경로중 가장 큰 통로를 최단거리에서 빼주었습니다.
'Algorithm > BOJ' 카테고리의 다른 글
저거 못 타면 지각이야!! - 15804 (0) 2020.03.16 주말 여행 계획 - 15808 (0) 2020.03.16 *빛*영*우* - 15807 (0) 2020.03.15 구간 합 구하기 - 2042 (0) 2020.03.15 영우의 기숙사 청소 - 15806 (0) 2020.03.15