forked from jpcsousa/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
executable file
·88 lines (75 loc) · 1.72 KB
/
Dijkstra.cpp
File metadata and controls
executable file
·88 lines (75 loc) · 1.72 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
79
80
81
82
83
84
85
86
87
88
#include<queue>
#include<iostream>
#include<math.h>
using namespace std;
#define MAXINT (int)(pow(2,31)-1)
#define MAX 100
int graph[MAX][MAX];
int total;
int distances[MAX];
int father[MAX];
bool visit[MAX];
void dijkstra(int start)
{
priority_queue<pair<int,int> > queue;
pair <int,int> nodotmp;
int i, j;
for (int i=1; i<=total; i++) {
distances[i] = MAXINT;
father[i] = -1;
visit[i] = false;
}
distances[start] = 0;
queue.push(pair <int,int> (distances[start], start));
while(!queue.empty()) {
nodotmp = queue.top();
queue.pop();
i = nodotmp.second;
if (!visit[i]) {
visit[i] = true;
for (j = 1; j<=total; j++)
if (!visit[j] && graph[i][j] > 0 && distances[i] + graph[i][j] < distances[j]) {
distances[j] = distances[i] + graph[i][j];
father[j] = i;
queue.push(pair <int,int>(-distances[j], j));
}
}
}
}
void getPath(int end) {
cout << end << " ";
while (father[end]!= -1) {
cout << father[end] << " ";
end = father[end];
}
cout << endl;
}
int main()
{
int a, b, c;
int tedges;
memset(graph, 0, sizeof(graph));
cin >> total >> tedges;
for (int i=0; i<tedges; i++) {
cin >> a >> b >> c;
graph[a][b] = c;
}
for(int i=1; i<=total; i++) {
for(int j=1; j<=total; j++)
printf("%d ", graph[i][j]);
printf("\n");
}
dijkstra(1);
getPath(3);
/*for (int i=1; i<=total; i++) {
dijkstra(i);
for(int i=1; i<=total; i++)
cout << distances[i] << " ";
cout << endl;
for(int i=1; i<=total; i++)
cout << father[i] << " ";
cout << endl;
getPath(5);
}*/
return 0;
}