-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicpc1916.cpp
More file actions
46 lines (44 loc) · 1.18 KB
/
icpc1916.cpp
File metadata and controls
46 lines (44 loc) · 1.18 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
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
using pint = pair<int, int>;
int dijkstra(int s, int t, vector<int> &dist, vector<vector<int> > &graph){
priority_queue<pint, vector<pint>, greater<pint> > pq;
dist[s] = 0;
pq.push(pint(s, dist[s]));
while(!pq.empty()){
int now = pq.top().second;
pq.pop();
if(now == t) break;
for(int next = 1; next < graph[now].size(); next++){
if(dist[next] > dist[now] + graph[now][next]){
dist[next] = dist[now] + graph[now][next];
pq.push(pint(dist[next],next));
}
}
}
return dist[t];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
const int INF = 1000000000;
int n, m, s, t;
vector<int> dist;
vector<vector<int> > graph;
cin >> n >> m;
dist.resize(n + 1, INF);
graph.resize(n + 1, vector<int>(n + 1, INF));
for(int i = 0; i < m ; i++){
int u, v, d;
cin >> u >> v >> d;
graph[u][v] = min(graph[u][v], d);
}
cin >> s >> t;
cout << dijkstra(s, t, dist, graph);
return 0;
}