#include <iostream> #include <set> #include <list> using namespace std; struct s { int id, g; }; s ns(int id, int g) { s r; r.id = id; r.g = g; return r; } bool operator< (s a, s b) { return a.id < b.id; } set<s> f[200001]; pair<int, int> sq[200000]; list<pair<int, int> > react; list<list<pair<int, int> >::iterator> tbr; long long int result = 0; void mv(int from, int to, set<s>::iterator fs, set<s>::iterator ts, list<pair<int, int> >::iterator r) { tbr.push_back(r); s ms = *fs; f[from].erase(fs); if (ms.g >= ts->g) { ms.g -= (ts->g); result += 2 * (ts->g); } else { result += 2 * ms.g; ms.g = (ts->g) - ms.g; ms.id = ts->id; } f[to].erase(ts); if (ms.g) { f[to].insert(ms); } } int main() { ios_base::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; s tmp; for (int i = 0; i < n; i++) { tmp.id = i+1; cin >> tmp.g; f[tmp.id].insert(tmp); } pair<int, int> tmp2; for (int i = 0; i < m; i++) { cin >> sq[i].first >> sq[i].second; } for (int i = 0; i < k; i++) { cin >> tmp2.first >> tmp2.second; react.push_back(tmp2); } list<pair<int, int> >::iterator it; set<s>::iterator as, bs; list<list<pair<int, int> >::iterator>::iterator tbrit; for (int i = 0; i < m; i++) { int from = sq[i].first; int to = sq[i].second; for (it = react.begin(); it != react.end(); it++) { int a = it->first; int b = it->second; as = f[from].find(ns(a, 0)); if (as != f[from].end()) { bs = f[to].find(ns(b, 0)); if (bs != f[to].end()) { mv(from, to, as, bs, it); } } else { as = f[to].find(ns(a, 0)); if (as != f[to].end()) { bs = f[from].find(ns(b, 0)); if (bs != f[from].end()) { mv(from, to, bs, as, it); } } } } for (tbrit = tbr.begin(); tbrit != tbr.end(); tbrit++) { react.erase(*tbrit); } tbr.clear(); for (as = f[from].begin(); as != f[from].end(); as++) { f[to].insert(*as); } } cout << result << endl; 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 97 98 99 100 101 102 103 104 105 | #include <iostream> #include <set> #include <list> using namespace std; struct s { int id, g; }; s ns(int id, int g) { s r; r.id = id; r.g = g; return r; } bool operator< (s a, s b) { return a.id < b.id; } set<s> f[200001]; pair<int, int> sq[200000]; list<pair<int, int> > react; list<list<pair<int, int> >::iterator> tbr; long long int result = 0; void mv(int from, int to, set<s>::iterator fs, set<s>::iterator ts, list<pair<int, int> >::iterator r) { tbr.push_back(r); s ms = *fs; f[from].erase(fs); if (ms.g >= ts->g) { ms.g -= (ts->g); result += 2 * (ts->g); } else { result += 2 * ms.g; ms.g = (ts->g) - ms.g; ms.id = ts->id; } f[to].erase(ts); if (ms.g) { f[to].insert(ms); } } int main() { ios_base::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; s tmp; for (int i = 0; i < n; i++) { tmp.id = i+1; cin >> tmp.g; f[tmp.id].insert(tmp); } pair<int, int> tmp2; for (int i = 0; i < m; i++) { cin >> sq[i].first >> sq[i].second; } for (int i = 0; i < k; i++) { cin >> tmp2.first >> tmp2.second; react.push_back(tmp2); } list<pair<int, int> >::iterator it; set<s>::iterator as, bs; list<list<pair<int, int> >::iterator>::iterator tbrit; for (int i = 0; i < m; i++) { int from = sq[i].first; int to = sq[i].second; for (it = react.begin(); it != react.end(); it++) { int a = it->first; int b = it->second; as = f[from].find(ns(a, 0)); if (as != f[from].end()) { bs = f[to].find(ns(b, 0)); if (bs != f[to].end()) { mv(from, to, as, bs, it); } } else { as = f[to].find(ns(a, 0)); if (as != f[to].end()) { bs = f[from].find(ns(b, 0)); if (bs != f[from].end()) { mv(from, to, bs, as, it); } } } } for (tbrit = tbr.begin(); tbrit != tbr.end(); tbrit++) { react.erase(*tbrit); } tbr.clear(); for (as = f[from].begin(); as != f[from].end(); as++) { f[to].insert(*as); } } cout << result << endl; return 0; } |