//Michal Wos MIM UW #include <climits> #include <cstdlib> #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #include <string> #include <string.h> #define pb push_back #define pii pair<int,int> #define vi vector<int> #define f first #define s second #define x first #define y second #define Size(x) ((int)(x).size()) #define FOR(z,b,e) for(__typeof(b) z = b; z<=e; z++ ) #define debon 1 #define deb(burak) if(debon) {cout<<"DEB-> "<<#burak<<": "<<burak<<endl;} #define debv(burak) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debt(burak,SIzE) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debend if(debon) {cout<<"_____________________"<<endl;} #define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));} using namespace std; typedef unsigned long long ULL; typedef long long LL; void readLL(LL *n) {register char c=0; while (c < 33) c=getc_unlocked(stdin); (*n)=0; while (c>32) {(*n)=(*n)*10LL + (c-'0'); c=getc_unlocked(stdin);}} const int inf=1073741824,mod=1e9+7; const int s=2010; int tab[s], ile[s]; int Find(int a) { if (tab[a] == a) { return a; } int fa = Find(tab[a]); tab[a] = fa; return fa; } bool Union(int a, int b) { int fa = Find(a); int fb = Find(b); if (fa == fb) { return false; } if (ile[fa] <= ile[fb]) { ile[fb] += ile[fa]; tab[fa] = fb; } else { ile[fa] += ile[fb]; tab[fb] = fa; } return true; } int main() { int n,m,k,a,b,c; ULL res = 0; scanf("%d%d%d",&n,&m,&k); int p[n + 1]; for ( int i = 1; i <= n; i++ ) { scanf("%d", p + i); // init F&U tab[i] = i; ile[i] = 1; } pii kolejnosc[m]; for ( int i = 0; i < m; i++ ) { scanf("%d%d", &kolejnosc[i].first, &kolejnosc[i].second); } pii osadowe[k]; for ( int i = 0; i < k; i++ ) { scanf("%d%d", &osadowe[i].first, &osadowe[i].second); if ( osadowe[i].second > osadowe[i].first ) { swap(osadowe[i].second, osadowe[i].first); } } for ( int i = 0; i < m; i++ ) { Union(kolejnosc[i].first, kolejnosc[i].second); for ( int j = 0; j < k; j++ ) { a = osadowe[j].first; b = osadowe[j].second; if ( p[a] > 0 && p[b] > 0 && (Find(a) == Find(b)) ) { c = min(p[a], p[b]); res += 2 * c; p[a] -= c; p[b] -= c; } } } printf("%llu\n",res); 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 | //Michal Wos MIM UW #include <climits> #include <cstdlib> #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #include <string> #include <string.h> #define pb push_back #define pii pair<int,int> #define vi vector<int> #define f first #define s second #define x first #define y second #define Size(x) ((int)(x).size()) #define FOR(z,b,e) for(__typeof(b) z = b; z<=e; z++ ) #define debon 1 #define deb(burak) if(debon) {cout<<"DEB-> "<<#burak<<": "<<burak<<endl;} #define debv(burak) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debt(burak,SIzE) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cout<<burak[zyx]<<" "; cout<<endl;} #define debend if(debon) {cout<<"_____________________"<<endl;} #define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));} using namespace std; typedef unsigned long long ULL; typedef long long LL; void readLL(LL *n) {register char c=0; while (c < 33) c=getc_unlocked(stdin); (*n)=0; while (c>32) {(*n)=(*n)*10LL + (c-'0'); c=getc_unlocked(stdin);}} const int inf=1073741824,mod=1e9+7; const int s=2010; int tab[s], ile[s]; int Find(int a) { if (tab[a] == a) { return a; } int fa = Find(tab[a]); tab[a] = fa; return fa; } bool Union(int a, int b) { int fa = Find(a); int fb = Find(b); if (fa == fb) { return false; } if (ile[fa] <= ile[fb]) { ile[fb] += ile[fa]; tab[fa] = fb; } else { ile[fa] += ile[fb]; tab[fb] = fa; } return true; } int main() { int n,m,k,a,b,c; ULL res = 0; scanf("%d%d%d",&n,&m,&k); int p[n + 1]; for ( int i = 1; i <= n; i++ ) { scanf("%d", p + i); // init F&U tab[i] = i; ile[i] = 1; } pii kolejnosc[m]; for ( int i = 0; i < m; i++ ) { scanf("%d%d", &kolejnosc[i].first, &kolejnosc[i].second); } pii osadowe[k]; for ( int i = 0; i < k; i++ ) { scanf("%d%d", &osadowe[i].first, &osadowe[i].second); if ( osadowe[i].second > osadowe[i].first ) { swap(osadowe[i].second, osadowe[i].first); } } for ( int i = 0; i < m; i++ ) { Union(kolejnosc[i].first, kolejnosc[i].second); for ( int j = 0; j < k; j++ ) { a = osadowe[j].first; b = osadowe[j].second; if ( p[a] > 0 && p[b] > 0 && (Find(a) == Find(b)) ) { c = min(p[a], p[b]); res += 2 * c; p[a] -= c; p[b] -= c; } } } printf("%llu\n",res); return 0; } |