-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEP6.cpp
More file actions
167 lines (139 loc) · 3.56 KB
/
EP6.cpp
File metadata and controls
167 lines (139 loc) · 3.56 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <vector>
#include <iterator>
#include <queue>
#include <limits>
#define endl "\n"
#define UNVISITED 0
#define VISITED 1
using namespace std;
typedef struct g{
vector<pair<int, int>> *l; // adjacency list
int numEdge; // number of edges
int n; // number of vertices
int *Mark; // auxiliary marking array
int *D; // distance from a vertex to all the others
} G;
G* create_graph(const int n);
int n(G* g);
int e(G* g);
int first(G* g, int v);
int next(G* g, int v, vector<pair<int, int>>::iterator &it);
int weight(G* g, vector<pair<int, int>>::iterator it);
void graphTraverse(G* g, int v);
void Dijkstra(G* g, int s);
void setEdge(G* g, int i, int j, int w);
void setMark(G* g, int v, int val);
int getMark(G* g, int v);
int main(void) {
int c;
cin >> c;
for(int i = 1; i <= c; i++) {
int v, a, s;
cin >> v >> a;
G* g = create_graph(v);
for(int k = 0; k < a; k++) {
int i, j, w;
cin >> i >> j >> w;
setEdge(g, i, j, w);
}
cin >> s;
graphTraverse(g, s);
cout << "Caso " << i << endl;
for(int k = 0; k < v; k++) {
if(g->D[k] < numeric_limits<int>::max()) {
cout << g->D[k] << endl;
}
else {
cout << -1 << endl;
}
}
}
return 0;
}
G* create_graph(int n) {
G* g = new G;
g->n = n;
g->Mark = new int[n];
g->D = new int[n];
g->l = new vector<pair<int, int>>[n];
g->numEdge = 0;
return g;
}
int n(G* g) {
return g->n;
}
int e(G* g) {
return g->numEdge;
}
int first(G* g, int v) {
if(g->l[v].empty()) {
return n(g);
}
return g->l[v].front().first;
}
int next(G* g, int v, vector<pair<int, int>>::iterator &it) {
it++;
if(it == g->l[v].end()) {
return n(g);
}
else {
return (*it).first;
}
}
int weight(G* g, vector<pair<int, int>>::iterator it) {
return (*it).second;
}
void graphTraverse(G* g, int v) {
for(int i = 0; i <= (n(g)-1); i++) {
setMark(g, i, UNVISITED);
}
if(getMark(g, v) == UNVISITED) {
Dijkstra(g, v);
}
}
void Dijkstra(G* g, int s) {
int *P = new int[g->n];
int p, v;
vector<pair<int, int>>::iterator it;
priority_queue<pair<int, pair<int,int>>, vector<pair<int, pair<int,int>>>, greater<pair<int, pair<int,int>>>> H;
for(int i = 0; i <= (n(g)-1); i++) {
g->D[i] = numeric_limits<int>::max();
P[i] = -1;
}
H.push(make_pair(0, make_pair(s, s)));
g->D[s] = 0;
for(int i = 0; i <= (n(g)-1); i++) {
do {
if(H.empty()) {
return;
}
pair<int, pair<int,int>> tmp = H.top();
H.pop();
p = tmp.second.first;
v = tmp.second.second;
} while(!(g->Mark[v] == UNVISITED));
setMark(g, v, VISITED);
P[v] = p;
int w = first(g, v);
it = g->l[v].begin();
while(w < n(g)) {
if(getMark(g, w) != VISITED && g->D[w] > g->D[v] + weight(g, it)) {
g->D[w] = g->D[v] + weight(g, it);
H.push(make_pair(g->D[w], make_pair(v, w)));
}
w = next(g, v, it);
}
}
}
void setEdge(G* g, int i, int j, int w) {
g->numEdge++;
g->l[i].push_back(make_pair(j, w));
//g->l[j].push_back(make_pair(i, w)); // only for undirected graphs
}
void setMark(G* g, int v, int val) {
g->Mark[v] = val;
}
int getMark(G* g, int v) {
return g->Mark[v];
}