-
최소비용 구하기 2 - 11779Algorithm/BOJ 2020. 3. 12. 03:1112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273//11779 - 최소비용 구하기2#include <iostream>#include <vector>#include <stack>#include <queue>using namespace std;#define INF 1000000000int N, M;vector<vector<pair<int, int>>> adj;int S, E;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));}cin >> S >> E;}void dijkstra(int start){vector<int> dist(N+1, INF);vector<int> parent(N+1);dist[start] = 0;parent[start] = start;priority_queue<pair<int, int>> pq;pq.push(make_pair(0, start));while(!pq.empty()){int here = pq.top().second;int cost = -pq.top().first;pq.pop();if(here != start && 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(dist[there] > cost + c){dist[there] = cost + c;parent[there] = here;pq.push(make_pair(-(cost+c), there));}}}cout << dist[E] << "\n";stack<int> s;for(int i = E; i != parent[i]; i = parent[i])s.push(i);s.push(S);cout << s.size() << "\n";while(!s.empty())cout << s.top() << " ", s.pop();}int main(){ios_base::sync_with_stdio(0); cin.tie(0);init();dijkstra(S);}
cs https://www.acmicpc.net/problem/11779
11779번: 최소비용 구하기 2
첫째 줄에 도시의 개수 n(1≤n≤1,000)이 주어지고 둘째 줄에는 버스의 개수 m(1≤m≤100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다. 그리고 그 다음에는 도착지의 도시 번호가 주어지고 또 그 버스 비용이 주어진다. 버스 비용은 0보다 크거나 같고, 100,000보다 작은 정수이다. 그리고 m+3째 줄에는 우리가 구하고자 하는 구간 출발점의 도시
www.acmicpc.net
최소비용 구하기 2
최단경로 비용과 그 경로를 출력하는 다익스트라 문제입니다.
'Algorithm > BOJ' 카테고리의 다른 글
전화번호 목록 - 5052 (0) 2020.03.12 최종 순위 - 3665 (0) 2020.03.12 열쇠 - 9328 (0) 2020.03.12 백조의 호수 - 3197 (0) 2020.03.11 학교 탐방하기 - 13418 (2) 2020.03.11