-
Notifications
You must be signed in to change notification settings - Fork 1
/
boj.1238.cc
92 lines (76 loc) · 1.84 KB
/
boj.1238.cc
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
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e9;
ll n, m, x, edges0[1000][1000] = {{0}}, edges1[1000][1000] = {{0}};
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m>>x;
--x;
for(ll from, to, t, i = 0; i<m; ++i)
{
cin>>to>>from>>t;
edges0[--from][--to] = t;
edges1[to][from] = t;
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
vector<ll> cost0(n, INF);
cost0[x] = 0;
pq.push(make_pair(0, x));
while(!pq.empty())
{
ll node = pq.top().second, t = pq.top().first;
pq.pop();
if(t != cost0[node])
{
continue;
}
for(ll newCost, next = 0; next<n; ++next)
{
if(!edges0[node][next])
{
continue;
}
newCost = t + edges0[node][next];
if(newCost < cost0[next])
{
pq.push(make_pair(cost0[next] = newCost, next));
}
}
}
vector<ll> cost1(n, INF);
cost1[x] = 0;
pq.push(make_pair(0, x));
while(!pq.empty())
{
ll node = pq.top().second, t = pq.top().first;
pq.pop();
if(t != cost1[node])
{
continue;
}
for(ll newCost, next = 0; next<n; ++next)
{
if(!edges1[node][next])
{
continue;
}
newCost = t + edges1[node][next];
if(newCost < cost1[next])
{
pq.push(make_pair(cost1[next] = newCost, next));
}
}
}
ll result = 0;
for(int i = 0; i<n; ++i)
{
result = max(result, cost0[i] + cost1[i]);
}
cout<<result<<"\n";
return 0;
}