#include<cstdio> #include<vector> #include<algorithm> #define MP make_pair #define fi first #define se second using namespace std; const int N = 200001, K = 500001; vector<pair<long long int, long long int> >v[N]; long long int n, m, k, i, x, a[N], b[N], c[K], d[K], w[N]; long long int osad; void przelej(int a, int b) { // printf("przelewamy %d do %d\n", a, b); for(int i = 0; i < v[b].size(); i++) { w[v[b][i].fi] = v[b][i].se; // printf("w[%d] = %d\n", v[b][i].fi, w[v[b][i].fi]); } for(int i = 0; i < v[a].size(); i++) { if (w[v[a][i].fi] == 0){v[b].push_back(MP(v[a][i].fi, v[a][i].se));} w[v[a][i].fi] += v[a][i].se; // printf("w[%d] = %d\n", v[a][i].fi, w[v[a][i].fi]); } for(int i = 0; i < k; i++) { if (w[c[i]] > 0 && w[d[i]] > 0) { osad += min(w[c[i]], w[d[i]]); // printf("w[%d] = %d w[%d] = %d osad %lld\n", c[i], w[c[i]], d[i], w[d[i]], osad); if (w[c[i]] > w[d[i]]) { w[c[i]] -= w[d[i]]; w[d[i]] = 0; } else { w[d[i]] -= w[c[i]]; w[c[i]] = 0; } } } for(int i = 0; i < v[b].size(); i++) { v[b][i].se = w[v[b][i].fi]; w[v[b][i].fi] = 0; } for(int i = v[b].size() - 1; i >= 0; i--) { if (v[b][i].se == 0) { swap(v[b][i].fi, v[b].back().fi); swap(v[b][i].se, v[b].back().se); i--; v[b].pop_back(); } } } int main() { scanf("%lld%lld%lld", &n, &m, &k); for(i = 1; i <= n; i++) { scanf("%lld", &x); v[i].push_back(MP(i, x)); } for(i = 0; i < m; i++) { scanf("%lld%lld", &a[i], &b[i]); } for(i = 0; i < k; i++) { scanf("%lld%lld", &c[i], &d[i]); } for(i = 0; i < m; i++) { przelej(a[i], b[i]); } printf("%lld\n", osad * 2); 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 | #include<cstdio> #include<vector> #include<algorithm> #define MP make_pair #define fi first #define se second using namespace std; const int N = 200001, K = 500001; vector<pair<long long int, long long int> >v[N]; long long int n, m, k, i, x, a[N], b[N], c[K], d[K], w[N]; long long int osad; void przelej(int a, int b) { // printf("przelewamy %d do %d\n", a, b); for(int i = 0; i < v[b].size(); i++) { w[v[b][i].fi] = v[b][i].se; // printf("w[%d] = %d\n", v[b][i].fi, w[v[b][i].fi]); } for(int i = 0; i < v[a].size(); i++) { if (w[v[a][i].fi] == 0){v[b].push_back(MP(v[a][i].fi, v[a][i].se));} w[v[a][i].fi] += v[a][i].se; // printf("w[%d] = %d\n", v[a][i].fi, w[v[a][i].fi]); } for(int i = 0; i < k; i++) { if (w[c[i]] > 0 && w[d[i]] > 0) { osad += min(w[c[i]], w[d[i]]); // printf("w[%d] = %d w[%d] = %d osad %lld\n", c[i], w[c[i]], d[i], w[d[i]], osad); if (w[c[i]] > w[d[i]]) { w[c[i]] -= w[d[i]]; w[d[i]] = 0; } else { w[d[i]] -= w[c[i]]; w[c[i]] = 0; } } } for(int i = 0; i < v[b].size(); i++) { v[b][i].se = w[v[b][i].fi]; w[v[b][i].fi] = 0; } for(int i = v[b].size() - 1; i >= 0; i--) { if (v[b][i].se == 0) { swap(v[b][i].fi, v[b].back().fi); swap(v[b][i].se, v[b].back().se); i--; v[b].pop_back(); } } } int main() { scanf("%lld%lld%lld", &n, &m, &k); for(i = 1; i <= n; i++) { scanf("%lld", &x); v[i].push_back(MP(i, x)); } for(i = 0; i < m; i++) { scanf("%lld%lld", &a[i], &b[i]); } for(i = 0; i < k; i++) { scanf("%lld%lld", &c[i], &d[i]); } for(i = 0; i < m; i++) { przelej(a[i], b[i]); } printf("%lld\n", osad * 2); return 0; } |