标签:main pre class printf 分享 ++ div alt 最大
最大流EK:
1 #include <vector> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define N1 210 6 #define M1 1010 7 #define ll long long 8 #define dd double 9 #define inf 0x3f3f3f3f 10 using namespace std; 11 12 int gint() 13 { 14 int ret=0,fh=1;char c=getchar(); 15 while(c<‘0‘||c>‘9‘){if(c==‘-‘)fh=-1;c=getchar();} 16 while(c>=‘0‘&&c<=‘9‘){ret=ret*10+c-‘0‘;c=getchar();} 17 return ret*fh; 18 } 19 20 struct Edge{ 21 int head[N1],to[M1<<1],nxt[M1<<1],val[M1<<1],cte; 22 void ae(int u,int v,int w) 23 { 24 cte++; to[cte]=v; val[cte]=w; 25 nxt[cte]=head[u]; head[u]=cte; 26 } 27 }e; 28 29 int que[N1],hd,tl; 30 int flow[N1],id[N1]; 31 32 int bfs(int S,int T) 33 { 34 int x,j,v; 35 memset(flow,0,sizeof(flow)); 36 memset(id,0,sizeof(id)); 37 hd=1,tl=0; que[++tl]=S; flow[S]=inf; 38 while(hd<=tl) 39 { 40 x=que[hd++]; 41 for(j=e.head[x];j;j=e.nxt[j]) 42 { 43 v=e.to[j]; 44 if(id[v]||e.val[j]==0) continue; 45 flow[v]=min(flow[x],e.val[j]); 46 id[v]=j; que[++tl]=v; 47 } 48 } 49 if(!flow[T]) return -1; 50 else return flow[T]; 51 } 52 53 int EK(int S,int T) 54 { 55 int x,mxflow=0,tmp; 56 while(1) 57 { 58 tmp=bfs(S,T); 59 if(tmp==-1) return mxflow; 60 for(x=T;x!=S;x=e.to[id[x]^1]) 61 { 62 e.val[id[x]]-=tmp; 63 e.val[id[x]^1]+=tmp; 64 } 65 mxflow+=tmp; 66 } 67 } 68 69 int n,m,S,T; 70 71 int main() 72 { 73 scanf("%d%d%d%d",&n,&m,&S,&T); 74 int i,j,k,x,y,z; e.cte=1; 75 for(i=1;i<=m;i++){ x=gint(); y=gint(); z=gint(); e.ae(x,y,z); e.ae(y,x,0); } 76 printf("%d\n",EK(S,T)); 77 return 0; 78 }
标签:main pre class printf 分享 ++ div alt 最大
原文地址:https://www.cnblogs.com/guapisolo/p/10268891.html