-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path80.cpp
More file actions
65 lines (54 loc) · 1.3 KB
/
Copy path80.cpp
File metadata and controls
65 lines (54 loc) · 1.3 KB
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
// Dijkstra
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define INF 2147000000
struct edge{
int v,val;
edge(int a, int b){
v = a;
val = b;
}
bool operator<(const edge& b)const{
return val > b.val;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("input.txt", "rt", stdin);
int n, m, a, b, c;
cin>>n>>m;
vector<int> dist(n + 1, INF);
vector<pair<int, int> > map[21];
priority_queue<edge> pq;
for(int i = 0; i < m; i++){
cin>>a>>b>>c;
map[a].push_back(make_pair(b, c));
}
pq.push(edge(1, 0));
dist[1] = 0;
while(!pq.empty()){
int now = pq.top().v;
int weight = pq.top().val;
pq.pop();
if(weight > dist[now]) continue;
for(int i = 0; i < map[now].size(); i++){
int next = map[now][i].first;
int nextwei = weight + map[now][i].second;
if(dist[next] > nextwei){
dist[next] = nextwei;
pq.push(edge(next, nextwei));
}
}
}
for(int i = 2; i < dist.size(); i++){
if(dist[i] == INF){
cout<<i<<" : imposssible"<<"\n";
}else{
cout<<i<<" : "<<dist[i]<<"\n";
}
}
return 0;
}