-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.cpp
69 lines (62 loc) · 1.31 KB
/
dijkstra.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
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<utility>
#define MAX 10000
const int INF = 0x3f3f3f3f;
using namespace std;
int n,
m,
processados[MAX],
distancia[MAX]; // distance from infinity
vector<int> vizinhos[MAX], edge[MAX];
void dijkstra(int s)
{
for(int i = 1; i <= n; ++i)
distancia[i] = INF;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
distancia[s] = 0;
pq.push(make_pair(distancia[s], s));
while(!pq.empty())
{
int u = pq.top().second;
pq.pop();
for(int i = 0; i < vizinhos[u].size(); ++i)
{
int v = vizinhos[u][i];
int w = edge[u][i];
if(distancia[v] > distancia[u] + w)
{
distancia[v] = distancia[u] + w;
processados[v] = u;
pq.push(make_pair(distancia[v], v));
}
}
}
}
// < 1.in
// 5 9 1 5
// 1 2 4
// 1 3 2
// 2 3 3
// 2 4 2
// 2 5 3
// 3 2 1
// 3 5 5
// 3 4 4
// 5 4 1
int main()
{
int m, fonte, s, x, y, w;
cin >> n >> m >> fonte >> s;
for(int i = 0; i < m; ++i)
{
cin >> x >> y >> w;
vizinhos[x].push_back(y);
edge[x].push_back(w);
}
dijkstra(fonte);
cout << distancia[3];
return 0;
}