-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47.spfa.cpp
More file actions
53 lines (51 loc) · 927 Bytes
/
47.spfa.cpp
File metadata and controls
53 lines (51 loc) · 927 Bytes
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<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> PII;
int h[N], e[N], w[N], ne[N], idx;
int dist[N];
int n, m;
bool st[N];
void add(int a, int b, int c){
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int spfa(){
queue<PII> q;
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
q.push({0, 1});
st[1] = true;
while(q.size()){
PII p = q.front();
q.pop();
int t = p.second;
st[t] = false;
for(int i = h[t]; i != -1; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
if(!st[j]){
st[j] = true;
q.push({dist[j], j});
}
}
}
}
return dist[n];
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
while(m--){
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
int res = spfa();
if(res == 0x3f3f3f3f) cout << "impossible" << endl;
else cout << res;
return 0;
}