-
Notifications
You must be signed in to change notification settings - Fork 0
/
16707.cpp
52 lines (47 loc) · 1.22 KB
/
16707.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
#include <bits/stdc++.h>
using namespace std;
const int MX = 1005;
const int inf = 987654321;
int N, M, s, e, d;
vector <pair<int, int> > adj[MX];
int SPFA(int S){
bool inQ[MX];
int dist[MX];
pair <int, int> bef[MX];
memset(inQ, 0, sizeof(inQ));
fill(dist + 1, dist + N + 1, inf);
queue <int> q;
q.push(S);
dist[S] = 0;
while(!q.empty()){
int pos = q.front(), i = 0;
q.pop();
for(auto tmp : adj[pos]){
int next = tmp.first, cost = tmp.second + dist[pos];
if(cost < dist[next]){
dist[next] = cost;
bef[next] = {pos, i};
if(!inQ[next]) q.push(next);
}
i++;
}
}
int pos = 2;
while(pos != S){
int prev = bef[pos].first, idx = bef[pos].second, cost = adj[prev][idx].second;
adj[prev].erase(adj[prev].begin() + idx);
adj[pos].push_back({prev, -cost});
pos = prev;
}
return dist[2];
}
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
cin >> N >> M;
for(int i = 0; i < M; i++){
cin >> s >> e >> d;
adj[s].push_back({e, d});
adj[e].push_back({s, d});
}
cout << SPFA(1) + SPFA(N);
}