-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wonderland.cpp
95 lines (86 loc) · 2.47 KB
/
Wonderland.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
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
/* https://www.spoj.com/problems/UCV2013B/
#shortest-path #bellman-ford
*/
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
class Edge {
public:
int u, v, dist;
Edge() {}
Edge(int _u, int _v, int _dist): u(_u), v(_v), dist(_dist) {}
};
bool BellmanFord(vector<Edge>& graph, int startVertex, int numVertex, vector<int> & distArr) {
// initilize
for(int i=0; i < distArr.size(); i++) {
distArr[i] = INT_MAX;
}
distArr[startVertex] = 0;
// relax edges repeatedly
for (int i=1; i <= numVertex -1; i++ ) {
for(Edge& e: graph) {
if (distArr[e.u] != INT_MAX && distArr[e.u] + e.dist < distArr[e.v]) {
distArr[e.v] = distArr[e.u] + e.dist;
}
}
}
// check negative-weight cycles
for(Edge& e: graph) {
if (distArr[e.u] != INT_MAX && distArr[e.u] + e.dist < distArr[e.v]) {
return false;
}
}
return true;
}
int main() {
int n;
int numcase = 1;
while(cin >> n) {
if (n == 0) break;
vector<string> monumentArr;
vector<Edge> graph;
for(int u=0; u < n; u++) {
string name;
cin >> name;
monumentArr.push_back(name);
for (int v=0; v < n; v++) {
int dist;
cin >> dist;
if (dist == 0 && u != v) continue;
Edge e(u, v, dist);
graph.push_back(e);
}
}
cout << "Case #" << numcase << ":" << endl;
int q;
cin >> q;
int prev_src = -1;
bool r = false;
vector<int> distArr(n, INT_MAX);
for(int i=0; i < q; i++) {
int src, dest;
cin >> src >> dest;
/* should use array to save
if distArr[src].size == 0
resize distArr
bellman
*/
if (prev_src != src) {
prev_src = src;
r = BellmanFord(graph, prev_src, n, distArr);
}
if (r) {
if (distArr[dest] == INT_MAX) {
cout << monumentArr[src] << "-" << monumentArr[dest] << " NOT REACHABLE" << endl;
} else {
cout << monumentArr[src] << "-" << monumentArr[dest] << " " << distArr[dest] << endl;
}
} else {
cout << "NEGATIVE CYCLE" << endl;
}
}
numcase++;
}
return 0;
}