-
Notifications
You must be signed in to change notification settings - Fork 0
/
1238.cpp
69 lines (63 loc) · 1.49 KB
/
1238.cpp
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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int INF = 10000000;
int n, m, x;
int dist[1001][1001] = {0, };
void input();
void dijkstra(int start);
int answer();
priority_queue<pair<int, int>>q;
int main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == j)continue;
if(dist[i][j] < INF)
q.push({-dist[i][j], j});
}
dijkstra(i);
}
cout << answer();
}
void input(){
cin >> n >> m >> x;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
dist[i][j] = INF;
}
}
for(int i = 1; i <= n; i++)
dist[i][i] = 0;
int tmp1, tmp2, tmp3;
for(int i = 0; i < m; i++){
cin >> tmp1 >> tmp2 >> tmp3;
dist[tmp1][tmp2] = tmp3;
}
}
//dist[start][1~n]
void dijkstra(int start){
while(!q.empty()){
int currentDist = -q.top().first;
int currentNode = q.top().second;
q.pop();
if(currentDist > dist[start][currentNode]) continue;
for(int i = 1; i <= n; i++){
if(dist[start][i] > currentDist+dist[currentNode][i]){
dist[start][i] = currentDist+dist[currentNode][i];
q.push({-dist[start][i], i});
}
}
}
}
int answer(){
int maxi = 0;
for(int i = 1; i <= n; i++){
maxi = max(maxi, dist[i][x]+dist[x][i]);
}
return maxi;
}