반응형
https://www.acmicpc.net/problem/15422
15422번: Bumped!
The input consists of a single test case. The first line lists five space-separated integers n, m, f, s, and t, denoting the number of cities n (0 < n ≤ 50 000), the number of roads m (0 ≤ m ≤ 150 000), the number of flights f (0 ≤ f ≤ 1 000), th
www.acmicpc.net
[ 문제풀이 ]
1. visited[ n ][ used ] 배열을 만들어 비행기 표를 썼을 때와 쓰지 않았을 때 이동 거리를 visited 배열에 저장합니다.
2. dijkstra를 활용하여 s부터 t까지 이동하고 이동거리를 출력합니다.
[ 소스코드 ]
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include<iostream> #include<vector> #include<queue> #define ll long long using namespace std; struct node { int cur; ll cost; int used; }; struct cmp { bool operator()(node right, node left) { return left.cost < right.cost; } }; int n, m, f, s, t; vector<pair<int, int>> list[50000]; ll visited[50000][2]; int main() { scanf("%d %d %d %d %d", &n, &m, &f, &s, &t); for (int i = 0; i < n; i++) { visited[i][0] = visited[i][1] = 3333333333; } for (int i = 0; i < m; i++) { int u, v, c; scanf("%d %d %d", &u, &v, &c); list[u].push_back({ v,c }); list[v].push_back({ u,c }); } for (int i = 0; i < f; i++) { int u, v; scanf("%d %d", &u, &v); list[u].push_back({ v,0 }); } priority_queue<node, vector<node>, cmp> pq; pq.push({ s,0,0 }); while (!pq.empty()) { const int cur = pq.top().cur; const ll cost = pq.top().cost; const int used = pq.top().used; pq.pop(); if (visited[cur][used] < cost) continue; visited[cur][used] = cost; if (cur == t) { printf("%lld", cost); return 0; } for (auto& next : list[cur]) { if (used == 0 && next.second == 0) { if (visited[next.first][1] > cost) { visited[next.first][1] = cost; pq.push({ next.first, cost, 1 }); } } else if (next.second != 0) { if (visited[next.first][used] > cost + next.second) { visited[next.first][used] = cost + next.second; pq.push({ next.first, visited[next.first][used], used}); } } } } } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 15989번 - 1, 2, 3 더하기 4 (C++) (0) | 2023.03.26 |
---|---|
[ 백준 ] 23286번 - 허들 넘기 (C++) (0) | 2023.03.25 |
[ 백준 ] 21940번 - 가운데에서 만나기 (C++) (0) | 2023.03.23 |
[ 백준 ] 14567번 - 선수과목 (Prerequisite) (C++) (0) | 2023.03.22 |
[ 백준 ] 4963번 - 섬의 개 (C++) (0) | 2023.03.21 |