-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy path[16]_2.cpp
More file actions
80 lines (75 loc) · 1.85 KB
/
[16]_2.cpp
File metadata and controls
80 lines (75 loc) · 1.85 KB
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 <bits/stdc++.h>
using namespace std;
int n, m, start, finish;
vector<pair<int, int> > adj[500];
int d[500];
vector<pair<int, int> > reverseAdj[500];
bool dropped[500][500];
void dijkstra() {
priority_queue<pair<int, int> > pq;
pq.push(make_pair(0, start));
d[start] = 0;
while (!pq.empty()) {
int dist = -pq.top().first; // 현재 노드까지의 비용
int now = pq.top().second; // 현재 노드
pq.pop();
if (d[now] < dist) continue;
for (int i = 0; i < adj[now].size(); i++) {
int next = adj[now][i].first; // 다음 노드
int nextDist = dist + adj[now][i].second; // 현재 노드를 거쳐 가는 비용
if (d[next] > nextDist && !dropped[now][next]) {
d[next] = nextDist;
pq.push(make_pair(-nextDist, next));
}
}
}
}
void bfs() {
queue<int> q;
q.push(finish);
while (!q.empty()) {
int now = q.front();
q.pop();
if (now == start) continue;
for (int i = 0; i < reverseAdj[now].size(); i++) {
int prev = reverseAdj[now][i].first;
int cost = reverseAdj[now][i].second;
// 최단 경로에 포함된 경우 체크하기
if (d[now] == d[prev] + cost) {
dropped[prev][now] = true;
q.push(prev);
}
}
}
}
int main() {
while (true) {
cin >> n >> m;
if (n == 0) break;
cin >> start >> finish;
for (int i = 0; i < n; i++) {
adj[i].clear();
reverseAdj[i].clear();
}
for (int i = 0; i < m; i++) {
int x, y, cost;
cin >> x >> y >> cost;
adj[x].push_back(make_pair(y, cost));
reverseAdj[y].push_back(make_pair(x, cost));
}
// false로 초기화할 때는 memset() 함수 이용
memset(dropped, false, sizeof(dropped));
// 특정한 값으로 초기화할 때는 fill() 함수 이용
fill(d, d + 500, 1e9);
dijkstra();
bfs();
fill(d, d + 500, 1e9);
dijkstra();
if (d[finish] != 1e9) {
cout << d[finish] << '\n';
}
else {
cout << -1 << '\n';
}
}
}