#ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #else #define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #endif #include <cstdlib> #include <ctime> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <algorithm> #include <string> #include <iostream> #define FOR(x,y,z) for (int x=(y); x<=(z); ++x) #define FORD(x,y,z) for(int x=(y); x>=(z); --x) #define REP(x,y) for (int x=0; x<(y); ++x) #if defined(__GNUC__) && __cplusplus < 201103L #define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y) #else #define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y) #endif #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((int)(x).size()) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; typedef vector<VPII> VVPII; const int INF = 1000000001; struct Problem { int n,m,k; VI g; VPII st; VVPII adj; vector<set<int> > t; Problem(int n, int m, int k) : n(n), m(m), k(k), g(n), st(m), adj(n), t(n) { } LL Go(); LL Next(int a, int b, int dest); }; LL Problem::Go() { REP(i,n) t[i].insert(i); LL res = 0; REP(i,m) { if (t[st[i].first].size() <= t[st[i].second].size()) res += Next(st[i].first, st[i].second, st[i].second); else res += Next(st[i].second, st[i].first, st[i].second); } return res*2; } LL Problem::Next(int a, int b, int dest) { LL res = 0; vector<pair<int,PII> > r; FOREACH(it1,t[a]) { FOREACH(it2,adj[*it1]) { if (t[b].find(it2->second) != t[b].end()) r.push_back(make_pair(it2->first, PII(*it1, it2->second))); } } t[b].insert(ALL(t[a])); t[a].clear(); if (dest == a) swap(t[a], t[b]); sort(ALL(r)); FOREACH(it,r) { int x = it->second.first; int y = it->second.second; int gg = min(g[x], g[y]); res += gg; g[x] -= gg; g[y] -= gg; } return res; } int main(int argc, char** argv) { int n,m,k; scanf("%d%d%d", &n, &m, &k); Problem p(n,m,k); REP(i,n) scanf("%d", &p.g[i]); REP(i,m) { int a,b; scanf("%d%d", &a, &b); p.st[i] = PII(--a, --b); } REP(i,k) { int a,b; scanf("%d%d", &a, &b); p.adj[--a].push_back(PII(i, --b)); p.adj[b].push_back(PII(i, a)); } printf("%lld\n", p.Go()); #ifdef _DEBUG system("pause"); #endif 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 | #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #else #define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #endif #include <cstdlib> #include <ctime> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <algorithm> #include <string> #include <iostream> #define FOR(x,y,z) for (int x=(y); x<=(z); ++x) #define FORD(x,y,z) for(int x=(y); x>=(z); --x) #define REP(x,y) for (int x=0; x<(y); ++x) #if defined(__GNUC__) && __cplusplus < 201103L #define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y) #else #define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y) #endif #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((int)(x).size()) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; typedef vector<VPII> VVPII; const int INF = 1000000001; struct Problem { int n,m,k; VI g; VPII st; VVPII adj; vector<set<int> > t; Problem(int n, int m, int k) : n(n), m(m), k(k), g(n), st(m), adj(n), t(n) { } LL Go(); LL Next(int a, int b, int dest); }; LL Problem::Go() { REP(i,n) t[i].insert(i); LL res = 0; REP(i,m) { if (t[st[i].first].size() <= t[st[i].second].size()) res += Next(st[i].first, st[i].second, st[i].second); else res += Next(st[i].second, st[i].first, st[i].second); } return res*2; } LL Problem::Next(int a, int b, int dest) { LL res = 0; vector<pair<int,PII> > r; FOREACH(it1,t[a]) { FOREACH(it2,adj[*it1]) { if (t[b].find(it2->second) != t[b].end()) r.push_back(make_pair(it2->first, PII(*it1, it2->second))); } } t[b].insert(ALL(t[a])); t[a].clear(); if (dest == a) swap(t[a], t[b]); sort(ALL(r)); FOREACH(it,r) { int x = it->second.first; int y = it->second.second; int gg = min(g[x], g[y]); res += gg; g[x] -= gg; g[y] -= gg; } return res; } int main(int argc, char** argv) { int n,m,k; scanf("%d%d%d", &n, &m, &k); Problem p(n,m,k); REP(i,n) scanf("%d", &p.g[i]); REP(i,m) { int a,b; scanf("%d%d", &a, &b); p.st[i] = PII(--a, --b); } REP(i,k) { int a,b; scanf("%d%d", &a, &b); p.adj[--a].push_back(PII(i, --b)); p.adj[b].push_back(PII(i, a)); } printf("%lld\n", p.Go()); #ifdef _DEBUG system("pause"); #endif return 0; } |