-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1238.cpp
More file actions
58 lines (55 loc) · 1.41 KB
/
BOJ_1238.cpp
File metadata and controls
58 lines (55 loc) · 1.41 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
#include <bits/stdc++.h>
using namespace std;
const int INF = 987654321;
int N, M, X, a, b, c, ret;
int dist[1002], ans[1002];
vector<pair<int, int>> adj[1002];
int main() {
cin >> N >> M >> X;
for (int i = 0; i < M; i++) {
cin >> a >> b >> c;
adj[a].push_back({ c, b });
}
fill(ans, ans + 1002, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({ 0, X });
ans[X] = 0;
while (pq.size()) {
int here = pq.top().second;
int here_dist = pq.top().first;
pq.pop();
if (ans[here] != here_dist) continue;
for (pair<int, int> there : adj[here]) {
int _dist = there.first;
int _there = there.second;
if (ans[_there] > _dist + ans[here]) {
ans[_there] = _dist + ans[here];
pq.push({ ans[_there], _there });
}
}
}
for (int i = 1; i <= N; i++) {
fill(dist, dist + 1002, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({ 0, i });
dist[i] = 0;
while (pq.size()) {
int here = pq.top().second;
int here_dist = pq.top().first;
pq.pop();
if (dist[here] != here_dist) continue;
for (auto there : adj[here]) {
int _dist = there.first;
int _there = there.second;
if (dist[_there] > _dist + dist[here]) {
dist[_there] = _dist + dist[here];
pq.push({ dist[_there], _there });
}
}
}
ans[i] += dist[X];
ret = max(ret, ans[i]);
}
cout << ret << "\n";
return 0;
}