-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj1238.cpp
94 lines (87 loc) · 2.18 KB
/
boj1238.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <functional>
#include <set>
#include <queue>
using namespace std;
using p = pair<int, int>;
using ll = long long;
const int MAX = 10e+6;
const int INF = 0x66554433;
inline void Quick_IO(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
vector<p> adj[1005];
vector<p> radj[1005];
int dist[1005];
int rdist[1005];
int N, M, X;
void dij(int start){
vector<bool> visited(N+1, false);
priority_queue<p, vector<p>, greater<>> pq;
pq.push({0, start});
dist[start] = 0;
while(!pq.empty()){
int curr;
do{
curr = pq.top().second;
pq.pop();
}while(!pq.empty() && visited[curr]);
if(visited[curr]) continue;
for (auto &x: adj[curr]) {
int nextIndex = x.first;
int nextDist = x.second;
if(dist[nextIndex]>nextDist+dist[curr]){
dist[nextIndex] = nextDist+dist[curr];
pq.push({dist[nextIndex], nextIndex});
}
}
}
}
void rdij(int start){
vector<bool> visited(N+1, false);
priority_queue<p, vector<p>, greater<>> pq;
pq.push({0, start});
rdist[start] = 0;
while(!pq.empty()){
int curr;
do{
curr = pq.top().second;
pq.pop();
}while(!pq.empty() && visited[curr]);
if(visited[curr]) continue;
for (auto &x: radj[curr]) {
int nextIndex = x.first;
int nextDist = x.second;
if(rdist[nextIndex]>nextDist+rdist[curr]){
rdist[nextIndex] = nextDist+rdist[curr];
pq.push({rdist[nextIndex], nextIndex});
}
}
}
}
int main(){
Quick_IO();
cin>>N>>M>>X;
fill(dist+1, dist+N+1, INF);
fill(rdist+1, rdist+N+1, INF);
for (int i = 0; i < M; ++i) {
int u, v, w;
cin>>u>>v>>w;
adj[u].emplace_back(v, w);
radj[v].emplace_back(u, w);
}
dij(X);
rdij(X);
int answer = -INF;
for (int i = 1; i <= N; ++i) {
answer = max(answer, dist[i]+rdist[i]);
}
cout<<answer<<"\n";
return 0;
}