-
Notifications
You must be signed in to change notification settings - Fork 0
/
1238.cpp
77 lines (62 loc) · 1.48 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
70
71
72
73
74
75
76
77
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#define N 1005
using namespace std;
typedef pair<int, int> pii;
int n, m, x;
vector<pii> map[N], remap[N];
int cost[N], tox[N];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i, j, a, b, c;
queue<int> q;
cin >> n >> m >> x;
for(i=1; i<=m; i++){
cin >> a >> b >> c;
map[a].push_back(make_pair(b, c));
remap[b].push_back(make_pair(a, c));
}
memset(cost, -1, sizeof(cost));
cost[x] = 0;
q.push(x);
while(!q.empty()){
int f = q.front();
q.pop();
for(j=0; j<map[f].size(); j++){
int idx = map[f][j].first;
int w = map[f][j].second;
if(cost[idx]>cost[f]+w || cost[idx]==-1){
cost[idx] = cost[f]+w;
q.push(idx);
}
}
}
for(i=1; i<=n; i++)
tox[i] = cost[i];
memset(cost, -1, sizeof(cost));
cost[x]=0;
q.push(x);
while(!q.empty()){
int f = q.front();
q.pop();
for(i=0; i<remap[f].size(); i++){
int idx = remap[f][i].first;
int w = remap[f][i].second;
if(cost[idx]>cost[f]+w || cost[idx]==-1){
cost[idx] = cost[f]+w;
q.push(idx);
}
}
}
int ans=0;
for(i=1; i<=n; i++){
ans = max(ans, cost[i]+tox[i]);
}
cout << ans;
return 0;
}