-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14284.cpp
More file actions
55 lines (47 loc) · 1.32 KB
/
14284.cpp
File metadata and controls
55 lines (47 loc) · 1.32 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
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define INF 1000
vector<int> dijkstra(int start, int N, vector<vector<pair<int, int> > > graph) {
vector<int> dist(N + 1, INF);
priority_queue<pair<int, int> > pq;
dist[start] = 0;
pq.push(make_pair(start, 0));
while (!pq.empty()) {
int cur_node = pq.top().first;
int cur_dist = -pq.top().second;
pq.pop();
for (int i = 0; i < graph[cur_node].size(); i++) {
int nxt_node = graph[cur_node][i].first;
int nxt_dist = cur_dist + graph[cur_node][i].second;
if (dist[nxt_node] > nxt_dist) {
dist[nxt_node] = nxt_dist;
pq.push(make_pair(nxt_node, -nxt_dist));
}
}
}
return dist;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
int m;
int start;
int end;
vector<int> dist;
cin >> n >> m;
vector<vector<pair<int, int> > > graph(n + 1);
for (int i = 0; i < m; i++) {
int from, to, cost;
cin >> from >> to >> cost;
graph[from].push_back(make_pair(to, cost));
graph[to].push_back(make_pair(from, cost));
}
cin >> start >> end;
dist = dijkstra(start, n, graph);
cout << dist[end] << "\n";
return 0;
}