#define NDEBUG #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <limits> #include <climits> #include <map> #include <set> #include <vector> #include <queue> #include <cassert> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); const int nMax = 200000, mMax = 200000, kMax = 500000; int n, m, k; struct Chemical; int Chemical_ix(const Chemical* a); struct Chemical { int g; int ix() const { return Chemical_ix(this); } static bool lessThanInt(const Chemical* a, int b) { return a->ix() < b; } }; Chemical chemicals[nMax]; int Chemical_ix(const Chemical* a) { return a - chemicals; } struct Step { int from; int to; }; Step steps[mMax]; struct DepositPair { int p; int q; static bool less(const DepositPair* a, const DepositPair* b) { return a < b; } }; DepositPair depositPairs[kMax]; struct DepositPairWithEndpoint { DepositPair* depair; int e; static bool less(const DepositPairWithEndpoint* a, const DepositPairWithEndpoint* b) { return (a->e != b->e) ? a->e < b->e : a->depair < b->depair; } static bool lessThanInt(const DepositPairWithEndpoint* a, int b) { return a->e < b; } }; DepositPairWithEndpoint depositPairWithEndpointBuf[2 * kMax]; DepositPairWithEndpoint* depositPairsByEndpoint[2 * kMax]; int depositPairsByEndpointLen; struct Fio { std::vector<Chemical*> chemicals; }; Fio fios[nMax]; Chemical* fioMerge[nMax]; DepositPair* forReaction[kMax]; int main() { std::ios_base::sync_with_stdio(false); scanf("%d %d %d", &n, &m, &k); depositPairsByEndpointLen = 2 * k; for (int i=0; i < n; ++i) { Chemical& chemical = chemicals[i]; Fio& fio = fios[i]; int g; scanf("%d", &g); chemical.g = g; fio.chemicals.push_back(&chemical); } for (int i=0; i < m; ++i) { Step& step = steps[i]; int a, b; scanf("%d %d", &a, &b); step.from = a - 1; step.to = b - 1; } for (int i=0; i < k; ++i) { DepositPair& depair = depositPairs[i]; DepositPairWithEndpoint& e1 = depositPairWithEndpointBuf[2 * i]; DepositPairWithEndpoint& e2 = depositPairWithEndpointBuf[2 * i + 1]; int c, d; scanf("%d %d", &c, &d); if (c < d) { depair.p = c - 1; depair.q = d - 1; } else { depair.p = d - 1; depair.q = c - 1; } e1.depair = &depair; e1.e = depair.p; e2.depair = &depair; e2.e = depair.q; depositPairsByEndpoint[2 * i] = &e1; depositPairsByEndpoint[2 * i + 1] = &e2; } std::sort(depositPairsByEndpoint, depositPairsByEndpoint + depositPairsByEndpointLen, DepositPairWithEndpoint::less); long long result = 0; for (int i=0; i < m; ++i) { Step& step = steps[i]; std::vector<Chemical*>& fromChemicals = fios[step.from].chemicals; std::vector<Chemical*>& toChemicals = fios[step.to].chemicals; bool fromLess = (fromChemicals.size() <= toChemicals.size()); std::vector<Chemical*>& fewer = (fromLess) ? fromChemicals : toChemicals; std::vector<Chemical*>& more = (!fromLess) ? fromChemicals : toChemicals; int forReactionLen = 0; for (std::vector<Chemical*>::iterator fIt=fewer.begin(); fIt != fewer.end(); ++fIt) { int fChIx = (*fIt)->ix(); DepositPairWithEndpoint** fPos = std::lower_bound(depositPairsByEndpoint, depositPairsByEndpoint + depositPairsByEndpointLen, fChIx, DepositPairWithEndpoint::lessThanInt); while (fPos != depositPairsByEndpoint + depositPairsByEndpointLen && (*fPos)->e == fChIx) { int mChIx = ((*fPos)->depair->p != fChIx) ? (*fPos)->depair->p : (*fPos)->depair->q; std::vector<Chemical*>::iterator mPos = std::lower_bound(more.begin(), more.end(), mChIx, Chemical::lessThanInt); if (mPos != more.end() && (*mPos)->ix() == mChIx) { forReaction[forReactionLen] = (*fPos)->depair; ++forReactionLen; } ++fPos; } } std::sort(forReaction, forReaction + forReactionLen, DepositPair::less); for (int reIx=0; reIx < forReactionLen; ++reIx) { DepositPair& depair = *forReaction[reIx]; Chemical& p = chemicals[depair.p]; Chemical& q = chemicals[depair.q]; bool pLess = (p.g <= q.g); Chemical& less = (pLess) ? p : q; Chemical& more = (!pLess) ? p : q; result += 2 * less.g; more.g -= less.g; less.g = 0; } Chemical** merge = fioMerge; std::vector<Chemical*>::iterator from = fromChemicals.begin(); std::vector<Chemical*>::iterator to = toChemicals.begin(); while (true) { bool fromAvailable = (from != fromChemicals.end()); bool toAvailable = (to != toChemicals.end()); if (fromAvailable && toAvailable) { if (*from < *to) { if ((*from)->g > 0) { *merge = *from; ++merge; } ++from; } else { if ((*to)->g > 0) { *merge = *to; ++merge; } ++to; } } else if (fromAvailable) { if ((*from)->g > 0) { *merge = *from; ++merge; } ++from; } else if (toAvailable) { if ((*to)->g > 0) { *merge = *to; ++merge; } ++to; } else { break; } } std::vector<Chemical*>().swap(fromChemicals); toChemicals.clear(); toChemicals.insert(toChemicals.end(), fioMerge, merge); } printf("%lld\n", result); }
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | #define NDEBUG #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <limits> #include <climits> #include <map> #include <set> #include <vector> #include <queue> #include <cassert> const long long LLMAX = (std::numeric_limits<long long>()).max(); const long long LLMIN = (std::numeric_limits<long long>()).min(); const int nMax = 200000, mMax = 200000, kMax = 500000; int n, m, k; struct Chemical; int Chemical_ix(const Chemical* a); struct Chemical { int g; int ix() const { return Chemical_ix(this); } static bool lessThanInt(const Chemical* a, int b) { return a->ix() < b; } }; Chemical chemicals[nMax]; int Chemical_ix(const Chemical* a) { return a - chemicals; } struct Step { int from; int to; }; Step steps[mMax]; struct DepositPair { int p; int q; static bool less(const DepositPair* a, const DepositPair* b) { return a < b; } }; DepositPair depositPairs[kMax]; struct DepositPairWithEndpoint { DepositPair* depair; int e; static bool less(const DepositPairWithEndpoint* a, const DepositPairWithEndpoint* b) { return (a->e != b->e) ? a->e < b->e : a->depair < b->depair; } static bool lessThanInt(const DepositPairWithEndpoint* a, int b) { return a->e < b; } }; DepositPairWithEndpoint depositPairWithEndpointBuf[2 * kMax]; DepositPairWithEndpoint* depositPairsByEndpoint[2 * kMax]; int depositPairsByEndpointLen; struct Fio { std::vector<Chemical*> chemicals; }; Fio fios[nMax]; Chemical* fioMerge[nMax]; DepositPair* forReaction[kMax]; int main() { std::ios_base::sync_with_stdio(false); scanf("%d %d %d", &n, &m, &k); depositPairsByEndpointLen = 2 * k; for (int i=0; i < n; ++i) { Chemical& chemical = chemicals[i]; Fio& fio = fios[i]; int g; scanf("%d", &g); chemical.g = g; fio.chemicals.push_back(&chemical); } for (int i=0; i < m; ++i) { Step& step = steps[i]; int a, b; scanf("%d %d", &a, &b); step.from = a - 1; step.to = b - 1; } for (int i=0; i < k; ++i) { DepositPair& depair = depositPairs[i]; DepositPairWithEndpoint& e1 = depositPairWithEndpointBuf[2 * i]; DepositPairWithEndpoint& e2 = depositPairWithEndpointBuf[2 * i + 1]; int c, d; scanf("%d %d", &c, &d); if (c < d) { depair.p = c - 1; depair.q = d - 1; } else { depair.p = d - 1; depair.q = c - 1; } e1.depair = &depair; e1.e = depair.p; e2.depair = &depair; e2.e = depair.q; depositPairsByEndpoint[2 * i] = &e1; depositPairsByEndpoint[2 * i + 1] = &e2; } std::sort(depositPairsByEndpoint, depositPairsByEndpoint + depositPairsByEndpointLen, DepositPairWithEndpoint::less); long long result = 0; for (int i=0; i < m; ++i) { Step& step = steps[i]; std::vector<Chemical*>& fromChemicals = fios[step.from].chemicals; std::vector<Chemical*>& toChemicals = fios[step.to].chemicals; bool fromLess = (fromChemicals.size() <= toChemicals.size()); std::vector<Chemical*>& fewer = (fromLess) ? fromChemicals : toChemicals; std::vector<Chemical*>& more = (!fromLess) ? fromChemicals : toChemicals; int forReactionLen = 0; for (std::vector<Chemical*>::iterator fIt=fewer.begin(); fIt != fewer.end(); ++fIt) { int fChIx = (*fIt)->ix(); DepositPairWithEndpoint** fPos = std::lower_bound(depositPairsByEndpoint, depositPairsByEndpoint + depositPairsByEndpointLen, fChIx, DepositPairWithEndpoint::lessThanInt); while (fPos != depositPairsByEndpoint + depositPairsByEndpointLen && (*fPos)->e == fChIx) { int mChIx = ((*fPos)->depair->p != fChIx) ? (*fPos)->depair->p : (*fPos)->depair->q; std::vector<Chemical*>::iterator mPos = std::lower_bound(more.begin(), more.end(), mChIx, Chemical::lessThanInt); if (mPos != more.end() && (*mPos)->ix() == mChIx) { forReaction[forReactionLen] = (*fPos)->depair; ++forReactionLen; } ++fPos; } } std::sort(forReaction, forReaction + forReactionLen, DepositPair::less); for (int reIx=0; reIx < forReactionLen; ++reIx) { DepositPair& depair = *forReaction[reIx]; Chemical& p = chemicals[depair.p]; Chemical& q = chemicals[depair.q]; bool pLess = (p.g <= q.g); Chemical& less = (pLess) ? p : q; Chemical& more = (!pLess) ? p : q; result += 2 * less.g; more.g -= less.g; less.g = 0; } Chemical** merge = fioMerge; std::vector<Chemical*>::iterator from = fromChemicals.begin(); std::vector<Chemical*>::iterator to = toChemicals.begin(); while (true) { bool fromAvailable = (from != fromChemicals.end()); bool toAvailable = (to != toChemicals.end()); if (fromAvailable && toAvailable) { if (*from < *to) { if ((*from)->g > 0) { *merge = *from; ++merge; } ++from; } else { if ((*to)->g > 0) { *merge = *to; ++merge; } ++to; } } else if (fromAvailable) { if ((*from)->g > 0) { *merge = *from; ++merge; } ++from; } else if (toAvailable) { if ((*to)->g > 0) { *merge = *to; ++merge; } ++to; } else { break; } } std::vector<Chemical*>().swap(fromChemicals); toChemicals.clear(); toChemicals.insert(toChemicals.end(), fioMerge, merge); } printf("%lld\n", result); } |