-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1238.cpp
56 lines (45 loc) · 1.16 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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define INF 999999999
int N, M, X;
vector<pair<int, int>> v[1000+1];
vector<int> dist(1000+1, INF);
void dijkstra(int start){
priority_queue<pair<int, int>> pq;
pq.push({0, start});
while (!pq.empty()){
int cost = -pq.top().first;
int cur = pq.top().second;
pq.pop();
for(int i = 0; i < v[cur].size(); i++){
int next = v[cur][i].first;
int nCost = v[cur][i].second;
if(dist[next] > cost + nCost){
dist[next] = cost + nCost;
pq.push({-dist[next], next});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> M >> X;
for(int i = 0; i < M; i++){
int start, end, cost;
cin >> start >> end >> cost;
v[start].push_back({end, cost});
}
dijkstra(X);
vector<int> dist_final = dist;
int ans = 0;
for(int i = 1; i <= N; i++){
if(i == X) continue;
dijkstra(i);
dist_final[i] += dist[X];
ans = max(ans, dist_final[i]);
}
cout << ans;
}