标签:ext ges tchar order set char 文件名 网络流24题 oid
是在51Nod上提交的。
1 #include<cstring> 2 #include<cmath> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstdio> 6 #include<queue> 7 8 #define N 207 9 #define INF 10000007 10 using namespace std; 11 inline int read() 12 { 13 int x=0,f=1;char ch=getchar(); 14 while(ch<‘0‘||ch>‘9‘){if (ch==‘-‘) f=-1;ch=getchar();} 15 while(ch>=‘0‘&&ch<=‘9‘){x=(x<<3)+(x<<1)+ch-‘0‘;ch=getchar();} 16 return f*x; 17 } 18 19 int n,m,S,T,ans; 20 int cnt=1,head[N],rea[N*N],val[N*N],next[N*N]; 21 int dis[N]; 22 23 void add(int u,int v,int fee) 24 { 25 next[++cnt]=head[u],head[u]=cnt; 26 rea[cnt]=v,val[cnt]=fee; 27 } 28 bool bfs() 29 { 30 for (int i=1;i<=T;i++)dis[i]=0; 31 queue<int>q;q.push(S); 32 dis[S]=1; 33 while(!q.empty()) 34 { 35 int u=q.front();q.pop(); 36 for (int i=head[u];i!=-1;i=next[i]) 37 { 38 int v=rea[i],fee=val[i]; 39 if (!dis[v]&&fee>0) 40 { 41 dis[v]=dis[u]+1; 42 if (v==T) return 1; 43 q.push(v); 44 } 45 } 46 } 47 return 0; 48 } 49 int dfs(int u,int MF) 50 { 51 int res=0; 52 if (u==T||MF==0) return MF; 53 for (int i=head[u];i!=-1;i=next[i]) 54 { 55 int v=rea[i],fee=val[i]; 56 if (dis[v]!=dis[u]+1) continue; 57 int x=dfs(v,min(MF,fee)); 58 if (x) 59 { 60 val[i]-=x,val[i^1]+=x; 61 MF-=x,res+=x; 62 if (res==MF) return res; 63 } 64 } 65 if (!res) dis[u]=0; 66 return res; 67 } 68 void Dinic() 69 { 70 while(bfs()) 71 { 72 int x=dfs(S,INF); 73 while(x) 74 { 75 ans+=x; 76 x=dfs(S,INF); 77 } 78 } 79 } 80 int main() 81 { 82 memset(head,-1,sizeof(head)); 83 n=read(),m=read(); 84 int x=read(),y=read(); 85 while(x!=-1||y!=-1) 86 { 87 add(x,y,1),add(y,x,0); 88 x=read(),y=read(); 89 } 90 S=n+m+1,T=n+m+2; 91 for (int i=1;i<=n;i++) add(S,i,1),add(i,S,0); 92 for (int i=n+1;i<=n+m;i++) add(i,T,1),add(T,i,0); 93 Dinic(); 94 if (!ans) printf("No Solution!\n"); 95 else printf("%d",ans); 96 }
标签:ext ges tchar order set char 文件名 网络流24题 oid
原文地址:http://www.cnblogs.com/fengzhiyuan/p/7922086.html