#include <vector> #include <map> #include <set> #include <cstdio> using namespace std; int main() { long n,m,k; long long res; vector<map<long, long long> > fiolki; long long osad = 0; vector<long> from; vector<long> to; vector<long> reac1; vector<long> reac2; map<long, set<long> > w; set<long> selected; set<long> sel1; set<long> sel2; scanf("%ld %ld %ld\n", &n, &m, &k); for (int i=0;i<n;i++) { long long g; scanf("%lld", &g); map<long, long long> fi; fi[i] = g; fiolki.push_back(fi); } for (int i=0;i<m;i++) { long f,t; scanf("%ld %ld\n", &f, &t); from.push_back(f-1); to.push_back(t-1); } for (int i=0; i<k; i++) { long r1,r2; scanf("%ld %ld\n", &r1, &r2); long rx = min(r1,r2)-1; long ry = max(r1,r2)-1; reac1.push_back(rx); reac2.push_back(ry); if(w.find(rx) == w.end()) { w[rx] = set<long>(); } w[rx].insert(i); if(w.find(ry) == w.end()) { w[ry] = set<long>(); } w[ry].insert(i); } for (int i=0; i<m; i++) { long f = from[i]; long t = to[i]; //printf("from %ld to %ld\n", f, t); map<long, long long> &map1 = fiolki[f]; map<long, long long> &map2 = fiolki[t]; for(map<long, long long>::iterator it = map1.begin(); it != map1.end(); ++it) { map<long, set<long> >::iterator f1 = w.find(it->first); if (f1 != w.end()) { //sa reakcje dla tej substancji for(set<long>::iterator jt=(f1->second).begin(); jt!=(f1->second).end(); jt++) { sel1.insert(*jt); } } } for(map<long, long long>::iterator it = map2.begin(); it != map2.end(); ++it) { map<long, set<long> >::iterator f2 = w.find(it->first); if (f2 != w.end()) { //sa reakcje dla tej substancji for(set<long>::iterator jt=(f2->second).begin(); jt!=(f2->second).end(); jt++) { sel2.insert(*jt); } } } if(sel1.size() < sel2.size()) { for(set<long>::iterator it=sel1.begin(); it!=sel1.end(); it++) { if(sel2.find(*it) != sel2.end()) { selected.insert(*it); } } } else { for(set<long>::iterator it=sel2.begin(); it!=sel2.end(); it++) { if(sel1.find(*it) != sel1.end()) { selected.insert(*it); } } } for(set<long>::iterator it=selected.begin();it != selected.end(); it++) { long j = *it; // printf("j %ld\n", *it); long r1 = reac1[j]; long r2 = reac2[j]; // printf("%ld %ld \n", r1,r2); map<long, long long>::iterator it1 = map1.find(r1); map<long, long long>::iterator it2 = map2.find(r2); if(it1!=map1.end() && it2!=map2.end()) { long long rg = min(it1->second, it2->second); it1->second -= rg; it2->second -= rg; if (it1->second == 0) { map1.erase(it1); } if (it2->second == 0) { map2.erase(it2); } osad += 2*rg; } it1 = map1.find(r2); it2 = map2.find(r1); if(it1!=map1.end() && it2!=map2.end()) { long long rg = min(it1->second, it2->second); it1->second -= rg; it2->second -= rg; if (it1->second == 0) { map1.erase(it1); } if (it2->second == 0) { map2.erase(it2); } osad += 2*rg; } } for (map<long, long long>::iterator it = map1.begin(); it != map1.end(); ++it) { map2[it->first] = it->second; } map1.clear(); sel1.clear(); sel2.clear(); selected.clear(); } printf("%lld\n", osad); 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 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 | #include <vector> #include <map> #include <set> #include <cstdio> using namespace std; int main() { long n,m,k; long long res; vector<map<long, long long> > fiolki; long long osad = 0; vector<long> from; vector<long> to; vector<long> reac1; vector<long> reac2; map<long, set<long> > w; set<long> selected; set<long> sel1; set<long> sel2; scanf("%ld %ld %ld\n", &n, &m, &k); for (int i=0;i<n;i++) { long long g; scanf("%lld", &g); map<long, long long> fi; fi[i] = g; fiolki.push_back(fi); } for (int i=0;i<m;i++) { long f,t; scanf("%ld %ld\n", &f, &t); from.push_back(f-1); to.push_back(t-1); } for (int i=0; i<k; i++) { long r1,r2; scanf("%ld %ld\n", &r1, &r2); long rx = min(r1,r2)-1; long ry = max(r1,r2)-1; reac1.push_back(rx); reac2.push_back(ry); if(w.find(rx) == w.end()) { w[rx] = set<long>(); } w[rx].insert(i); if(w.find(ry) == w.end()) { w[ry] = set<long>(); } w[ry].insert(i); } for (int i=0; i<m; i++) { long f = from[i]; long t = to[i]; //printf("from %ld to %ld\n", f, t); map<long, long long> &map1 = fiolki[f]; map<long, long long> &map2 = fiolki[t]; for(map<long, long long>::iterator it = map1.begin(); it != map1.end(); ++it) { map<long, set<long> >::iterator f1 = w.find(it->first); if (f1 != w.end()) { //sa reakcje dla tej substancji for(set<long>::iterator jt=(f1->second).begin(); jt!=(f1->second).end(); jt++) { sel1.insert(*jt); } } } for(map<long, long long>::iterator it = map2.begin(); it != map2.end(); ++it) { map<long, set<long> >::iterator f2 = w.find(it->first); if (f2 != w.end()) { //sa reakcje dla tej substancji for(set<long>::iterator jt=(f2->second).begin(); jt!=(f2->second).end(); jt++) { sel2.insert(*jt); } } } if(sel1.size() < sel2.size()) { for(set<long>::iterator it=sel1.begin(); it!=sel1.end(); it++) { if(sel2.find(*it) != sel2.end()) { selected.insert(*it); } } } else { for(set<long>::iterator it=sel2.begin(); it!=sel2.end(); it++) { if(sel1.find(*it) != sel1.end()) { selected.insert(*it); } } } for(set<long>::iterator it=selected.begin();it != selected.end(); it++) { long j = *it; // printf("j %ld\n", *it); long r1 = reac1[j]; long r2 = reac2[j]; // printf("%ld %ld \n", r1,r2); map<long, long long>::iterator it1 = map1.find(r1); map<long, long long>::iterator it2 = map2.find(r2); if(it1!=map1.end() && it2!=map2.end()) { long long rg = min(it1->second, it2->second); it1->second -= rg; it2->second -= rg; if (it1->second == 0) { map1.erase(it1); } if (it2->second == 0) { map2.erase(it2); } osad += 2*rg; } it1 = map1.find(r2); it2 = map2.find(r1); if(it1!=map1.end() && it2!=map2.end()) { long long rg = min(it1->second, it2->second); it1->second -= rg; it2->second -= rg; if (it1->second == 0) { map1.erase(it1); } if (it2->second == 0) { map2.erase(it2); } osad += 2*rg; } } for (map<long, long long>::iterator it = map1.begin(); it != map1.end(); ++it) { map2[it->first] = it->second; } map1.clear(); sel1.clear(); sel2.clear(); selected.clear(); } printf("%lld\n", osad); return 0; } |