-
Notifications
You must be signed in to change notification settings - Fork 9
/
10461-Difference.cpp
69 lines (64 loc) · 1.5 KB
/
10461-Difference.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
#include <bits/stdc++.h>
using namespace std;
int fastio() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); return 0; }
int __fastio = fastio();
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
// dfs will work too
int bfs(vector<vector<int>>& graph, int start, vector<int>& cost) {
int res = 0;
int n = graph.size();
vector<bool> vis(n);
queue<int> q;
vis[start] = true;
q.push(start);
while(!q.empty()) {
int top = q.front(); q.pop();
res += cost[top];
for(int x : graph[top])
if(!vis[x]) {
vis[x] = true;
q.push(x);
}
}
return res;
}
void solve() {
int v, e;
int tc=1;
while(cin >> v >> e, (v+e)) {
vector<int> vals(v);
for(int i=0;i<v;i++) cin >> vals[i];
vector<vector<int>> graph(v), rgraph(v);
for(int i=0;i<e;i++) {
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
rgraph[b].push_back(a);
}
int q, t;
cin >> q;
int totalCost = accumulate(all(vals), 0);
cout << "Case #" << tc++ << ":\n";
while(q--) {
cin >> t;
t--;
// earliest cost is all dependencies that have to finish before t
int costBefore = bfs(rgraph, t, vals) - vals[t];
int costAfter = bfs(graph, t, vals);
// latest cost is allCost excluding dependecies that depends on t directly/indirectly
int latest = totalCost - costAfter;
cout << latest - costBefore << "\n";
}
cout << "\n";
}
}
int main() {
int t=1;
while(t--) {
solve();
}
}