-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
70 lines (53 loc) · 1.28 KB
/
dijkstra.cpp
File metadata and controls
70 lines (53 loc) · 1.28 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N=100010,INFINITO=999999999;
vector<pii> vizinhos[N];
int processado[N]={};
int distancia[N];
int n;
void Dijkstra(int S){
for(int i = 1;i <= n;i++) distancia[i] = INFINITO;
distancia[S] = 0;
priority_queue< pii, vector<pii>, greater<pii> > fila;
fila.push( pii(distancia[S], S) );
while(true){
int davez = -1;
int menor = INFINITO;
while(!fila.empty()){
int atual = fila.top().second;
fila.pop();
if(!processado[atual]){
davez = atual;
break;
}
}
if(davez == -1) break;
processado[davez] = true;
for(int i = 0;i < (int)vizinhos[davez].size();i++){
int dist = vizinhos[davez][i].first;
int atual = vizinhos[davez][i].second;
if( distancia[atual] > distancia[davez] + dist ){
distancia[atual] = distancia[davez] + dist;
fila.push( pii(distancia[atual], atual) );
}
}
}}
int main()
{
int k;
scanf("%d %d", &n, &k);
for (int i=1;i<=k;i++){
int a,b,c;
scanf("%d %d %d", &a, &b, &c);
vizinhos[a].push_back(make_pair(c,b));
vizinhos[b].push_back(make_pair(c,a));
}
int x,y;
scanf("%d %d", &x, &y);
Dijkstra(x);
printf("%d\n", distancia[y]);
return 0;
}