-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
78 lines (67 loc) · 1.39 KB
/
dijkstra.cpp
File metadata and controls
78 lines (67 loc) · 1.39 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
71
72
73
74
75
76
77
78
//TIMING: 4:40
5 1 5
1 2 7
1 4 2
2 4 2
2 3 1
3 5 5
4 2 3
4 3 8
4 5 5
5 3 4
#include <bits/stdc++.h>
#define MAX 10000
#define INF 1<<30
using namespace std;
struct cmp{
operator()(pair<int,int> a, pair<int,int> b){
return a.second > b.second;
}
};
int n,o,dest,a,b,c;
vector< int > d(MAX, INF);
vector< bool > v(MAX, false);
vector< int > p(MAX);
priority_queue< pair<int,int>, vector< pair<int,int> > , cmp> Q;
vector< vector< pair<int,int> > > lista(MAX);
void relax(int act, int ady, int peso){
if(peso + d[act] < d[ady]){
d[ady]=peso + d[act];
p[ady]=act;
Q.push(make_pair(ady, d[ady]));
}
}
void dijkstra(){
int act,ady,peso;
Q.push(make_pair(o,0));
d[o]=0;
while(!Q.empty()){
act = Q.top().first;
Q.pop();
if(v[act]) continue;
v[act] = true;
for(int i=0;i<lista[act].size();i++){
ady = lista[act][i].first;
peso = lista[act][i].second;
if(!v[ady])
relax(act,ady,peso);
}
}
}
void escribir(int a){
if(p[a] != a)
escribir(p[a]);
cout << a << ' ';
}
int main(){
ifstream fin("in.txt");
fin >> n >> o >> dest;
for(int i=1;i<=n;i++)
p[i]=i;
while(fin >> a >> b >> c){
lista[a].push_back(make_pair(b,c));
lista[b].push_back(make_pair(a,c));
}
dijkstra();
escribir(dest);
}