uva 753(网络流最大流)
生活随笔
收集整理的這篇文章主要介紹了
uva 753(网络流最大流)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
網(wǎng)絡流最大流問題,這里使s=0,使s與所有的插頭相連,最大通量為1,然后插頭和轉換器相連,最大通量為1,轉換器和轉換器相連,因為有無限個,所以為inf,然后轉換器和插座連,最大通量為1,插座和t相連,最大通量為1,注意這里的插頭插座都是string,不是char,比如插頭可以是AB(我日李賴賴,寫了我一上午!!!!!
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> using namespace std; const int maxm=1000+10; const int inf=0x3f3f3f3f; string c[maxm],a[maxm]; struct note1 {string c1,c2; }; struct note {int fr,to,c,f;note(int a,int b,int x,int y):fr(a),to(b),c(x),f(y) {} }; struct Karp {vector<note> aa;vector<int> g[maxm];int a[maxm],p[maxm];void init(int n){for(int i=0; i<n; i++)g[i].clear();aa.clear();}void addedge(int x,int y,int z){aa.push_back(note(x,y,z,0));aa.push_back(note(y,x,0,0));int mm=aa.size();g[x].push_back(mm-2);g[y].push_back(mm-1);}int maxflow(int s,int t){int flow=0;while(1){memset(a,0,sizeof(a));queue<int> q;q.push(s);a[s]=inf;while(q.size()){int x=q.front();q.pop();for(int i=0; i<g[x].size(); i++){note &e=aa[g[x][i]];if(!a[e.to]&&(e.c>e.f)){a[e.to]=min(a[x],e.c-e.f);p[e.to]=g[x][i];q.push(e.to);}}if(a[t]) break;}if(!a[t]) break;for(int i=t; i!=s; i=aa[p[i]].fr){aa[p[i]].f+=a[t];aa[p[i]^1].f-=a[t];}flow+=a[t];}return flow;}}; Karp mmp; note1 b[maxm]; int t,n,m,k; int main() {scanf("%d",&t);while(t--){mmp.init(1000);scanf("%d",&n);for(int i=0; i<n; i++)cin >> c[i];scanf("%d",&m);string aa;for(int i=0; i<m; i++)cin>> aa >> a[i];scanf("%d",&k);for(int i=0; i<k; i++){cin >> b[i].c1 >> b[i].c2;}for(int i=0; i<m; i++){mmp.addedge(0,i+1,1);for(int j=0; j<k; j++){if(b[j].c1==a[i]){mmp.addedge(i+1,m+j+1,1);}}for(int j=0; j<n; j++)if(a[i]==c[j]) mmp.addedge(i+1,m+k+j+1,1);}for(int i=0; i<k; i++){for(int j=0; j<n; j++)if(b[i].c2==c[j]){mmp.addedge(m+i+1,m+k+j+1,1);}for(int j=0; j<k; j++)if(i!=j&&b[i].c2==b[j].c1){mmp.addedge(m+i+1,m+j+1,inf);}}int hh=m+k+n+1;for(int i=0; i<n; i++){int zz=m+k+i+1;mmp.addedge(zz,hh,1);}printf("%d\n",m-mmp.maxflow(0,hh));if(t) printf("\n");}return 0; }?
)
轉載于:https://www.cnblogs.com/Wangwanxiang/p/7284358.html
總結
以上是生活随笔為你收集整理的uva 753(网络流最大流)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Aliyun OSS Nginx pr
- 下一篇: readonly和disabled的区别