#include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; long long osad; int n, m, k; int a, b, c, d; int ilosc[2000]; int x[2000][2000]; vector <pair <int, int> > w; vector <int> v[2000]; struct xxx{ int a, b; int p; }tmp; struct cmp{ bool operator () (xxx a, xxx b){ return a.p<b.p; } }; set <xxx, cmp> secik; set <xxx, cmp>::iterator it; int roza, rozb; void lacz(int a, int b){ if(x[a][b]!=0){ tmp.a=a;tmp.b=b;tmp.p=x[a][b]; secik.insert(tmp); } roza=v[a].size(); rozb=v[b].size(); for(int i=0; i<roza; i++){ for(int j=0; j<rozb; j++){ if(x[v[a][i]][v[b][j]]!=0){ tmp.a=v[a][i];tmp.b=v[b][j];tmp.p=x[v[a][i]][v[b][j]]; secik.insert(tmp); } v[v[a][i]].push_back(v[b][j]); v[v[b][j]].push_back(v[a][i]); } } for(int i=0; i<roza; i++){ if(x[v[a][i]][b]!=0){ tmp.a=v[a][i];tmp.b=b;tmp.p=x[v[a][i]][b]; secik.insert(tmp); } v[v[a][i]].push_back(b); v[b].push_back(v[a][i]); } for(int i=0; i<rozb; i++){ if(x[a][v[b][i]]!=0){ tmp.a=a;tmp.b=v[b][i];tmp.p=x[a][v[b][i]]; secik.insert(tmp); } v[a].push_back(v[b][i]); v[v[b][i]].push_back(a); } v[a].push_back(b); v[b].push_back(a); } int minimum; void tworz_osad(){ while(!secik.empty()){ it=secik.begin(); minimum=min(ilosc[it->a], ilosc[it->b]); osad=osad+2*minimum; ilosc[it->a]-=minimum; ilosc[it->b]-=minimum; secik.erase(it); } } int main(){ ios_base::sync_with_stdio(0); cin>>n>>m>>k; for(int i=1; i<=n; i++) cin>>ilosc[i]; for(int i=0; i<m; i++){ cin>>a>>b; w.push_back(make_pair(a, b)); } for(int i=1; i<=k; i++){ cin>>c>>d; x[c][d]=i; x[d][c]=i; } for(int i=0; i<m; i++){ lacz(w[i].first, w[i].second); tworz_osad(); } cout<<osad<<"\n"; 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 | #include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; long long osad; int n, m, k; int a, b, c, d; int ilosc[2000]; int x[2000][2000]; vector <pair <int, int> > w; vector <int> v[2000]; struct xxx{ int a, b; int p; }tmp; struct cmp{ bool operator () (xxx a, xxx b){ return a.p<b.p; } }; set <xxx, cmp> secik; set <xxx, cmp>::iterator it; int roza, rozb; void lacz(int a, int b){ if(x[a][b]!=0){ tmp.a=a;tmp.b=b;tmp.p=x[a][b]; secik.insert(tmp); } roza=v[a].size(); rozb=v[b].size(); for(int i=0; i<roza; i++){ for(int j=0; j<rozb; j++){ if(x[v[a][i]][v[b][j]]!=0){ tmp.a=v[a][i];tmp.b=v[b][j];tmp.p=x[v[a][i]][v[b][j]]; secik.insert(tmp); } v[v[a][i]].push_back(v[b][j]); v[v[b][j]].push_back(v[a][i]); } } for(int i=0; i<roza; i++){ if(x[v[a][i]][b]!=0){ tmp.a=v[a][i];tmp.b=b;tmp.p=x[v[a][i]][b]; secik.insert(tmp); } v[v[a][i]].push_back(b); v[b].push_back(v[a][i]); } for(int i=0; i<rozb; i++){ if(x[a][v[b][i]]!=0){ tmp.a=a;tmp.b=v[b][i];tmp.p=x[a][v[b][i]]; secik.insert(tmp); } v[a].push_back(v[b][i]); v[v[b][i]].push_back(a); } v[a].push_back(b); v[b].push_back(a); } int minimum; void tworz_osad(){ while(!secik.empty()){ it=secik.begin(); minimum=min(ilosc[it->a], ilosc[it->b]); osad=osad+2*minimum; ilosc[it->a]-=minimum; ilosc[it->b]-=minimum; secik.erase(it); } } int main(){ ios_base::sync_with_stdio(0); cin>>n>>m>>k; for(int i=1; i<=n; i++) cin>>ilosc[i]; for(int i=0; i<m; i++){ cin>>a>>b; w.push_back(make_pair(a, b)); } for(int i=1; i<=k; i++){ cin>>c>>d; x[c][d]=i; x[d][c]=i; } for(int i=0; i<m; i++){ lacz(w[i].first, w[i].second); tworz_osad(); } cout<<osad<<"\n"; return 0; } |