-
특정한 최단 경로 - 1504Algorithm/BOJ 2020. 2. 18. 01:431234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162//1504 - 특정한 최단 경로#include <iostream>#include <vector>#include <queue>using namespace std;#define INF 200000000int N, M, U, V;vector<vector<pair<int, int>>> adj;void init(){cin >> N >> M;adj = vector<vector<pair<int, int>>>(N+1);int x, y, r;for(int i = 0; i<M; ++i){cin >> x >> y >> r;adj[x].push_back(make_pair(y, r));adj[y].push_back(make_pair(x, r));}cin >> U >> V;}int dijkstra(int start, int end){priority_queue<pair<int, int>> pq;vector<int> cost(N+1, INF);pq.push(make_pair(0, start));cost[start] = 0;while(!pq.empty()){int here = pq.top().second;int base = -pq.top().first;pq.pop();if(cost[here] == base)for(int i = 0; i<adj[here].size(); ++i){int there = adj[here][i].first;int Cost = adj[here][i].second + base;if(Cost < cost[there]){cost[there] = Cost;pq.push(make_pair(-Cost, there));}}}return cost[end];}int main(){ios_base::sync_with_stdio(0);cin.tie(0);init();int t = min(dijkstra(1, U) + dijkstra(V, N), dijkstra(1, V) + dijkstra(U, N)) + dijkstra(U, V);cout << (t >= INF ? -1 : t);}
cs https://www.acmicpc.net/problem/1504
1504번: 특정한 최단 경로
첫째 줄에 정점의 개수 N과 간선의 개수 E가 주어진다. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) 둘째 줄부터 E개의 줄에 걸쳐서 세 개의 정수 a, b, c가 주어지는데, a번 정점에서 b번 정점까지 양방향 길이 존재하며, 그 거리가 c라는 뜻이다. (1 ≤ c ≤ 1,000) 다음 줄에는 반드시 거쳐야 하는 두 개의 서로 다른 정점 번호가 주어진다.
www.acmicpc.net
특정한 최단 경로
기본 다익스트라 문제입니다
'Algorithm > BOJ' 카테고리의 다른 글
DFS와 BFS (0) 2020.02.18 거의 최단 경로 - 5719 (0) 2020.02.18 집합 - 11723 (0) 2020.02.17 제국 - 16402 (0) 2020.02.14 1602 - 도망자 원숭이 (0) 2020.02.14