-
Notifications
You must be signed in to change notification settings - Fork 0
/
1916.cpp
50 lines (39 loc) · 1.06 KB
/
1916.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
#include<iostream>
#include<queue>
#include<vector>
#define N 1005
#define INF 1000000000
using namespace std;
int n, m, s, e, dist[N];
typedef pair<int, int> pii;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pii>> qq;
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i, a, b, c;
scanf("%d %d", &n, &m);
vector<vector<pair<int, int>>> graph(n+5);
for(i=1; i<=m; i++){
scanf("%d %d %d", &a, &b, &c);
graph[a].push_back(make_pair(b, c));
}
scanf("%d %d", &s, &e);
for(i=1; i<=n; i++)
dist[i]=INF;
dist[s]=0;
qq.push(make_pair(s, 0));
while(!qq.empty()){
pair<int, int> head=qq.top();
qq.pop();
if(dist[head.first]<head.second) continue;
for(i=0; i<graph[head.first].size(); i++){
if(dist[graph[head.first][i].first]>head.second+graph[head.first][i].second){
dist[graph[head.first][i].first]=head.second+graph[head.first][i].second;
qq.push(make_pair(graph[head.first][i].first, dist[graph[head.first][i].first]));
}
}
}
printf("%d\n", dist[e]);
return 0;
}