#include <cstdio>
#include <unordered_map>
#include <vector>
#include <set>
#include <utility>
#include <algorithm>
using namespace std;
typedef unordered_map<int,int> UMII;
struct R {
int o, a, b;
R() : o(-1), a(0), b(0) {}
R(int o, int a, int b) : o(o), a(a), b(b) {}
};
struct R_comp {
bool operator() (const R &r1, const R &r2) const { return r1.o < r2.o; }
};
typedef set<R,R_comp> SR;
typedef vector<R> VR;
struct P {
UMII g;
SR r;
};
typedef vector<P> VP;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef long long ll;
int n, m, k, a, b;
VP p;
VPII s;
VR ar;
ll o;
int main() {
scanf("%d %d %d", &n, &m, &k);
p.resize(n + 1);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a);
p[i].g[i] = a;
}
s.resize(m);
for (int i = 0; i < m; ++i) {
scanf("%d %d", &a, &b);
s[i] = make_pair(a, b);
}
ar.resize(k);
for (int i = 1; i <= k; ++i) {
scanf("%d %d", &a, &b);
p[a].r.insert(R(i, a, b));
p[b].r.insert(R(i, a, b));
}
for (int i = 0; i < m; ++i) {
a = s[i].first;
b = s[i].second;
p[b].g.insert(p[a].g.begin(), p[a].g.end());
p[a].g.clear();
VR::iterator ar_end = set_intersection(
p[a].r.begin(), p[a].r.end(),
p[b].r.begin(), p[b].r.end(),
ar.begin(), R_comp());
for (VR::iterator it = ar.begin(); it != ar_end; ++it) {
if (p[b].g[it->a] < p[b].g[it->b]) {
o += 2LL * p[b].g[it->a];
p[b].g[it->b] -= p[b].g[it->a];
p[b].g[it->a] = 0;
} else {
o += 2LL * p[b].g[it->b];
p[b].g[it->a] -= p[b].g[it->b];
p[b].g[it->b] = 0;
}
if (p[b].g[it->a] == 0)
p[b].g.erase(it->a);
if (p[b].g[it->b] == 0)
p[b].g.erase(it->b);
}
p[b].r.insert(p[a].r.begin(), p[a].r.end());
p[a].r.clear();
for (SR::iterator it = p[b].r.begin(); it != p[b].r.end(); )
if (p[b].g.find(it->a) == p[b].g.end() && p[b].g.find(it->b) == p[b].g.end())
p[b].r.erase(it++);
else
++it;
}
printf("%lld\n", o);
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 | #include <cstdio> #include <unordered_map> #include <vector> #include <set> #include <utility> #include <algorithm> using namespace std; typedef unordered_map<int,int> UMII; struct R { int o, a, b; R() : o(-1), a(0), b(0) {} R(int o, int a, int b) : o(o), a(a), b(b) {} }; struct R_comp { bool operator() (const R &r1, const R &r2) const { return r1.o < r2.o; } }; typedef set<R,R_comp> SR; typedef vector<R> VR; struct P { UMII g; SR r; }; typedef vector<P> VP; typedef pair<int,int> PII; typedef vector<PII> VPII; typedef long long ll; int n, m, k, a, b; VP p; VPII s; VR ar; ll o; int main() { scanf("%d %d %d", &n, &m, &k); p.resize(n + 1); for (int i = 1; i <= n; ++i) { scanf("%d", &a); p[i].g[i] = a; } s.resize(m); for (int i = 0; i < m; ++i) { scanf("%d %d", &a, &b); s[i] = make_pair(a, b); } ar.resize(k); for (int i = 1; i <= k; ++i) { scanf("%d %d", &a, &b); p[a].r.insert(R(i, a, b)); p[b].r.insert(R(i, a, b)); } for (int i = 0; i < m; ++i) { a = s[i].first; b = s[i].second; p[b].g.insert(p[a].g.begin(), p[a].g.end()); p[a].g.clear(); VR::iterator ar_end = set_intersection( p[a].r.begin(), p[a].r.end(), p[b].r.begin(), p[b].r.end(), ar.begin(), R_comp()); for (VR::iterator it = ar.begin(); it != ar_end; ++it) { if (p[b].g[it->a] < p[b].g[it->b]) { o += 2LL * p[b].g[it->a]; p[b].g[it->b] -= p[b].g[it->a]; p[b].g[it->a] = 0; } else { o += 2LL * p[b].g[it->b]; p[b].g[it->a] -= p[b].g[it->b]; p[b].g[it->b] = 0; } if (p[b].g[it->a] == 0) p[b].g.erase(it->a); if (p[b].g[it->b] == 0) p[b].g.erase(it->b); } p[b].r.insert(p[a].r.begin(), p[a].r.end()); p[a].r.clear(); for (SR::iterator it = p[b].r.begin(); it != p[b].r.end(); ) if (p[b].g.find(it->a) == p[b].g.end() && p[b].g.find(it->b) == p[b].g.end()) p[b].r.erase(it++); else ++it; } printf("%lld\n", o); return 0; } |
English