forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstrasUsingVectors.cpp
60 lines (60 loc) · 1.38 KB
/
DijkstrasUsingVectors.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
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
using namespace std;
int n, m, S, T, w;
vector<vii> AdjList;
vi dist;
priority_queue<ii, vector<ii>, greater<ii>> pq;
void dijkstra(int s)
{
dist.assign(n, 1e9);
dist[s] = 0;
pq.push(ii(0, s));
while (!pq.empty())
{
ii front = pq.top(); pq.pop();
int d = front.first, u = front.second;
if (d > dist[u])
continue;
for (int j = 0; j < (int)AdjList[u].size(); j++)
{
ii v = AdjList[u][j];
if (dist[u] + v.second < dist[v.first])
{
dist[v.first] = dist[u] + v.second;
pq.push(ii(dist[v.first], v.first));
}
}
}
if (dist[T] == 1e9)
cout << "unreachable" << endl;
else
cout << dist[T] << endl;
}
int main()
{
int numCases;
cin >> numCases;
while (numCases--)
{
scanf("%d %d %d %d", &n, &m, &S, &T);
AdjList.assign(n, vii());
int n1, n2;
for (int i = 0; i < m; i++)
{
scanf("%d %d %d", &n1, &n2, &w);
AdjList[n1].push_back(ii(n2, w));
AdjList[n2].push_back(ii(n1, w));
}
dijkstra(S);
}
return 0;
}