#include <cstdio>
#include <algorithm>
#include <utility>
#include <set>
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
#define MAX_N 200000
#define MAX_K 500000
struct reaction {
int prio;
int first;
int second;
bool operator<(const reaction &other) const {
return prio < other.prio;
}
};
int main() {
int n, m, k, a, b, weight;
int vials[MAX_N+1];
std::pair<int, int> steps[MAX_N];
std::set<reaction> reactions[MAX_N+1];
std::set<reaction>::iterator it;
reaction r;
long long result = 0;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &vials[i]);
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
steps[i] = std::make_pair(a, b);
}
for (int i = 0; i < k; i++) {
scanf("%d %d", &a, &b);
r.prio = i;
r.first = a;
r.second = b;
reactions[a].insert(r);
reactions[b].insert(r);
}
for (int i = 0; i < m; i++) {
a = steps[i].first;
b = steps[i].second;
if (reactions[a].size() > reactions[b].size())
std::swap(reactions[a], reactions[b]);
// 'a' has less reactions
for (it = reactions[a].begin(); it != reactions[a].end(); it++) {
if (reactions[b].insert(*it).second == false) {
// same reaction in both sets
weight = MIN(vials[it->first], vials[it->second]);
result += 2 * weight;
vials[it->first] -= weight;
vials[it->second] -= weight;
}
}
reactions[a].clear();
}
printf("%lld\n", 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 | #include <cstdio> #include <algorithm> #include <utility> #include <set> #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) #define MAX_N 200000 #define MAX_K 500000 struct reaction { int prio; int first; int second; bool operator<(const reaction &other) const { return prio < other.prio; } }; int main() { int n, m, k, a, b, weight; int vials[MAX_N+1]; std::pair<int, int> steps[MAX_N]; std::set<reaction> reactions[MAX_N+1]; std::set<reaction>::iterator it; reaction r; long long result = 0; scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= n; i++) scanf("%d", &vials[i]); for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); steps[i] = std::make_pair(a, b); } for (int i = 0; i < k; i++) { scanf("%d %d", &a, &b); r.prio = i; r.first = a; r.second = b; reactions[a].insert(r); reactions[b].insert(r); } for (int i = 0; i < m; i++) { a = steps[i].first; b = steps[i].second; if (reactions[a].size() > reactions[b].size()) std::swap(reactions[a], reactions[b]); // 'a' has less reactions for (it = reactions[a].begin(); it != reactions[a].end(); it++) { if (reactions[b].insert(*it).second == false) { // same reaction in both sets weight = MIN(vials[it->first], vials[it->second]); result += 2 * weight; vials[it->first] -= weight; vials[it->second] -= weight; } } reactions[a].clear(); } printf("%lld\n", result); return 0; } |
English