forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10449 Traffic.cpp
157 lines (124 loc) · 3.71 KB
/
10449 Traffic.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
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
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
const long long Inf = LLONG_MAX;
struct Edge
{
int from, to, cost;
};
// Possible for n = 0!!
vector<int> busyness;
vector<long long> distances;
vector<Edge> allEdges;
vector<Edge> allEdgesByNode[205];
int predecessor[205];
bool inNegativeCycle[205];
const int White = 0, Gray = 1, Black = 2;
int color[205];
void PushAlongNegativeCycle(int node, long long newCost)
{
if (inNegativeCycle[node])
return;
inNegativeCycle[node] = true;
for (int e = 0; e < allEdgesByNode[node].size(); ++e)
{
int toNode = allEdgesByNode[node][e].to;
if (newCost + allEdgesByNode[node][e].cost < distances[toNode])
PushAlongNegativeCycle(toNode, newCost + allEdgesByNode[node][e].cost);
}
}
int ExpandPredecessor(int node)
{
if (color[node] != White)
return color[node];
color[node] = Gray;
// Not part of a cycle at all
if (predecessor[node] == -1)
return color[node] = Black;
int newColor = ExpandPredecessor(predecessor[node]);
inNegativeCycle[node] = (newColor == Gray);
return color[node] = newColor;
}
void ExpandNegativeCycle(int node)
{
inNegativeCycle[node] = true;
for (int e = 0; e < allEdgesByNode[node].size(); ++e)
{
int travelTo = allEdgesByNode[node][e].to;
if (!inNegativeCycle[travelTo])
ExpandNegativeCycle(travelTo);
}
}
void BellmanFord(int V, int E, int source)
{
distances.resize(V);
for (int i = 0; i < V; ++i)
distances[i] = (i == source) ? 0 : Inf;
for (int i = 0; i < V; ++i)
predecessor[i] = -1;
// Relax edges
for (int i = 0; i < V - 1; ++i)
{
for (int e = 0; e < E; ++e)
{
const Edge &edge = allEdges[e];
if (distances[edge.from] != Inf && distances[edge.from] + edge.cost < distances[edge.to])
{
distances[edge.to] = distances[edge.from] + edge.cost;
predecessor[edge.to] = edge.from;
}
}
}
// Go along the predecessor graph
for (int i = 0; i < V; ++i)
color[i] = White;
// Find all nodes that are part of a negative cycle
for (int i = 0; i < V; ++i)
ExpandPredecessor(i);
// Now, expand from all nodes that are in a negative cycle - they cause all children to become negative cycle nodes
for (int i = 0; i < V; ++i)
if (inNegativeCycle[i])
ExpandNegativeCycle(i);
}
int main()
{
int V;
int T = 1;
while (cin >> V)
{
busyness.resize(V);
for (int i = 0; i < V; ++i)
{
cin >> busyness[i];
inNegativeCycle[i] = false;
allEdgesByNode[i].clear();
}
int E;
cin >> E;
allEdges.resize(E);
for (int i = 0; i < E; ++i)
{
cin >> allEdges[i].from >> allEdges[i].to;
--allEdges[i].from;
--allEdges[i].to;
int diff = busyness[allEdges[i].to] - busyness[allEdges[i].from];
allEdges[i].cost = diff * diff * diff;
allEdgesByNode[allEdges[i].from].push_back(allEdges[i]);
}
BellmanFord(V, E, 0);
int Q;
cin >> Q;
cout << "Set #" << T++ << '\n';
for (int i = 0; i < Q; ++i)
{
int to;
cin >> to;
--to;
if (distances[to] < 3 || inNegativeCycle[to] || distances[to] == Inf)
cout << "?\n";
else
cout << distances[to] << '\n';
}
}
}