跑的是比Dinic快辣。
更新:指针版。。。。
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 using namespace std; 10 const int maxn=100000+10,maxm=2000000+10,inf=-1u>>1; 11 struct ISAP{ 12 struct ted{int x,y,w;ted*nxt,*re;}adj[maxm],*fch[maxn],*cur[maxn],*ret[maxn],*ms; 13 int gap[maxn],d[maxn],S,T,n; 14 void init(int n){ 15 this->n=n;ms=adj;memset(d,-1,sizeof(d));return; 16 } 17 void ade(int u,int v,int w){ 18 *ms=(ted){u,v,w,fch[u],ms+1};fch[u]=ms++; 19 *ms=(ted){v,u,0,fch[v],ms-1};fch[v]=ms++; 20 return; 21 } 22 void bfs(){ 23 queue<int>Q;Q.push(T);d[T]=0; 24 while(!Q.empty()){ 25 int u=Q.front();Q.pop(); 26 for(ted*e=fch[u];e;e=e->nxt){ 27 int v=e->y;if(d[v]<0) d[v]=d[u]+1,Q.push(v); 28 } 29 } return; 30 } 31 int flow(int S,int T){ 32 int k=S,flow=0;ted*e; 33 for(int i=1;i<=n;i++) cur[i]=fch[i]; 34 while(d[S]<n){ 35 if(k==T){ 36 int mi=inf; 37 for(int i=S;i!=T;i=cur[i]->y) if(cur[i]->w<mi) mi=cur[i]->w; 38 for(int i=S;i!=T;i=cur[i]->y) cur[i]->w-=mi,cur[i]->re->w+=mi; 39 flow+=mi;k=S; 40 } 41 for(e=cur[k];e;e=e->nxt) if(e->w>0&&d[k]==d[e->y]+1) break; 42 if(e) cur[k]=e,ret[e->y]=e->re,k=e->y; 43 else{ 44 if(--gap[d[k]]==0) break;cur[k]=fch[k];int lim=n; 45 for(ted*e=fch[k];e;e=e->nxt) if(e->w&&d[e->y]<lim) lim=d[e->y]; 46 d[k]=lim+1;gap[d[k]]++;if(k!=S) k=ret[k]->y; 47 } 48 } return flow; 49 } 50 }sol; 51 inline int read(){ 52 int x=0,sig=1;char ch=getchar(); 53 while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();} 54 while(isdigit(ch))x=10*x+ch-'0',ch=getchar(); 55 return x*=sig; 56 } 57 inline void write(int x){ 58 if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x; 59 int len=0,buf[15];while(x)buf[len++]=x%10,x/=10; 60 for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return; 61 } 62 int n,m; 63 void init(){ 64 n=read();m=read();sol.init(n); 65 int x,y,w; 66 while(m--) x=read(),y=read(),w=read(),sol.ade(x,y,w); 67 write(sol.flow(1,n)); 68 return; 69 } 70 void work(){ 71 return; 72 } 73 void print(){ 74 return; 75 } 76 int main(){init();work();print();return 0;}
数组版:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<queue> 6 #include<cstring> 7 #define PAU putchar(' ') 8 #define ENT putchar('\n') 9 using namespace std; 10 const int maxn=100000+10,maxm=2000000+10,inf=-1u>>1; 11 struct ISAP{ 12 struct tedge{int x,y,w,next;}adj[maxm];int ms,fch[maxn]; 13 int gap[maxn],s[maxn],cur[maxn],d[maxn],S,T,top,n; 14 void init(int n){ 15 this->n=n;ms=0;top=0; 16 memset(fch,-1,sizeof(fch)); 17 memset(d,-1,sizeof(d)); 18 return; 19 } 20 void addedge(int w,int v,int u){ 21 adj[ms]=(tedge){u,v,w,fch[u]};fch[u]=ms++; 22 adj[ms]=(tedge){v,u,0,fch[v]};fch[v]=ms++; 23 return; 24 } 25 void bfs(){ 26 queue<int>Q;Q.push(T);d[T]=0; 27 while(!Q.empty()){ 28 int u=Q.front();Q.pop(); 29 for(int i=fch[u];i!=-1;i=adj[i].next){ 30 int v=adj[i].y; 31 if(d[v]==-1) d[v]=d[u]+1,Q.push(v); 32 } 33 } return; 34 } 35 int maxflow(int S,int T){ 36 this->S=S;this->T=T;bfs();int k=S,i,flow=0; 37 for(i=1;i<=n;i++) cur[i]=fch[i],gap[d[i]]++; 38 while(d[S]<n){ 39 if(k==n){ 40 int mi=inf,pos; 41 for(i=0;i<top;i++) if(adj[s[i]].w<mi) mi=adj[s[i]].w,pos=i; 42 for(i=0;i<top;i++) adj[s[i]].w-=mi,adj[s[i]^1].w+=mi; 43 flow+=mi;top=pos;k=adj[s[top]].x; 44 } 45 for(i=cur[k];i!=-1;i=adj[i].next){ 46 int v=adj[i].y; 47 if(adj[i].w&&d[k]==d[v]+1){cur[k]=i;k=v;s[top++]=i;break;} 48 } 49 if(i==-1){ 50 int lim=n; 51 for(i=fch[k];i!=-1;i=adj[i].next){ 52 int v=adj[i].y; 53 if(adj[i].w&&d[v]<lim) lim=d[v],cur[k]=i; 54 } if(--gap[d[k]]==0) break; 55 d[k]=lim+1;gap[d[k]]++; 56 if(k!=S) k=adj[s[--top]].x; 57 } 58 } return flow; 59 } 60 }sol; 61 inline int read(){ 62 int x=0,sig=1;char ch=getchar(); 63 while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} 64 while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); 65 return x*=sig; 66 } 67 inline void write(int x){ 68 if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; 69 int len=0,buf[15];while(x) buf[len++]=x%10,x/=10; 70 for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; 71 } 72 void init(){ 73 int n=read(),m=read();sol.init(n); 74 while(m--) sol.addedge(read(),read(),read()); 75 write(sol.maxflow(1,n)); 76 return; 77 } 78 void work(){ 79 return; 80 } 81 void print(){ 82 return; 83 } 84 int main(){ 85 init();work();print();return 0; 86 }