https://www.acmicpc.net/problem/1753
풀이:
[C++]다익스트라 알고리즘(Dijkstra Algorithm) 참고
코드:
사용언어 : c++
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
|
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int V, E, k, u, v, w, i;
vector<int> d;
priority_queue<pair<int, int>> p;
bool b[20002];
int main() {
cin >> V >> E >> k;
vector<vector<pair<int, int>>>a(V + 1);
for (i = 0; i <= V; i++)
d.push_back(987654321);
for (i = 0; i < E; i++) {
cin >> u >> v >> w;
a[u].push_back({ v,w });
}
d[k] = 0;
p.push({ 0, k });
while (!p.empty()) {
w = p.top().second;
p.pop();
if (b[w]) continue;
b[w] = true;
for (auto t : a[w]) {
u = t.first, v = t.second;
if (d[u] > d[w] + v) {
d[u] = d[w] + v;
p.push({ d[u], u });
}
}
}
for (i = 1; i <= V; i++)
if (d[i] >= 987654321) cout << "INF" << endl;
else cout << d[i] << endl;
}
|