#include <cstdio> #include <map> #include <vector> #include <utility> #include <list> #include <algorithm> using namespace std; vector<int> phials; map<long long int, int> reactingPairs; vector<list<int> > currentState; long long int processStep(int a, int b) { long long int result = 0; long long int key; map<long long int, int>::iterator reaction; map<int, long long int> reactions; for (list<int>::iterator it1 = currentState[a].begin(); it1 != currentState[a].end(); ++it1) { for (list<int>::iterator it2 = currentState[b].begin(); it2 != currentState[b].end(); ++it2) { key = (*it2 > *it1) ? (long long int)*it2 * 1000000 + *it1 : (long long int)*it1 * 1000000 + *it2; reaction = reactingPairs.find(key); if (reaction != reactingPairs.end()) { reactions[reaction->second] = reaction->first; reactingPairs.erase(reaction); } } } for (map<int, long long int>::iterator it = reactions.begin(); it != reactions.end(); ++it) { int f = it->second % 1000000; int s = it->second / 1000000; int g = min(phials[f], phials[s]); phials[f] -= g; phials[s] -= g; result += 2*g; } for (list<int>::iterator it = currentState[b].begin(); it != currentState[b].end();) { if (phials[*it] == 0) { list<int>::iterator curr = it; ++it; currentState[b].erase(curr); } else { ++it; } } while (currentState[a].begin() != currentState[a].end()) { if (phials[*currentState[a].begin()] > 0) { currentState[b].push_back(*currentState[a].begin()); } currentState[a].erase(currentState[a].begin()); } return result; } int main() { int n, m, k; int a, b; scanf("%d %d %d", &n, &m, &k); phials.push_back(0); for (int i = 0; i < n; ++i) { scanf("%d", &a); phials.push_back(a); } vector<pair<int, int> > steps; for (int i = 0; i < m; ++i) { scanf("%d %d", &a, &b); steps.push_back(make_pair(a, b)); } for (int i = 0; i < k; ++i) { scanf("%d %d", &a, &b); long long int key; key = (a > b) ? (long long int)a * 1000000 + b : (long long int)b * 1000000 + a; reactingPairs[key] = i; } for (int i = 0; i <= n; ++i) { list<int> li; li.push_back(i); currentState.push_back(li); } long long int result = 0; for (int i = 0; i < m; ++i) { result += processStep(steps[i].first, steps[i].second); } printf("%lld", result); return 0; }
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 | #include <cstdio> #include <map> #include <vector> #include <utility> #include <list> #include <algorithm> using namespace std; vector<int> phials; map<long long int, int> reactingPairs; vector<list<int> > currentState; long long int processStep(int a, int b) { long long int result = 0; long long int key; map<long long int, int>::iterator reaction; map<int, long long int> reactions; for (list<int>::iterator it1 = currentState[a].begin(); it1 != currentState[a].end(); ++it1) { for (list<int>::iterator it2 = currentState[b].begin(); it2 != currentState[b].end(); ++it2) { key = (*it2 > *it1) ? (long long int)*it2 * 1000000 + *it1 : (long long int)*it1 * 1000000 + *it2; reaction = reactingPairs.find(key); if (reaction != reactingPairs.end()) { reactions[reaction->second] = reaction->first; reactingPairs.erase(reaction); } } } for (map<int, long long int>::iterator it = reactions.begin(); it != reactions.end(); ++it) { int f = it->second % 1000000; int s = it->second / 1000000; int g = min(phials[f], phials[s]); phials[f] -= g; phials[s] -= g; result += 2*g; } for (list<int>::iterator it = currentState[b].begin(); it != currentState[b].end();) { if (phials[*it] == 0) { list<int>::iterator curr = it; ++it; currentState[b].erase(curr); } else { ++it; } } while (currentState[a].begin() != currentState[a].end()) { if (phials[*currentState[a].begin()] > 0) { currentState[b].push_back(*currentState[a].begin()); } currentState[a].erase(currentState[a].begin()); } return result; } int main() { int n, m, k; int a, b; scanf("%d %d %d", &n, &m, &k); phials.push_back(0); for (int i = 0; i < n; ++i) { scanf("%d", &a); phials.push_back(a); } vector<pair<int, int> > steps; for (int i = 0; i < m; ++i) { scanf("%d %d", &a, &b); steps.push_back(make_pair(a, b)); } for (int i = 0; i < k; ++i) { scanf("%d %d", &a, &b); long long int key; key = (a > b) ? (long long int)a * 1000000 + b : (long long int)b * 1000000 + a; reactingPairs[key] = i; } for (int i = 0; i <= n; ++i) { list<int> li; li.push_back(i); currentState.push_back(li); } long long int result = 0; for (int i = 0; i < m; ++i) { result += processStep(steps[i].first, steps[i].second); } printf("%lld", result); return 0; } |