dinic模板。。。
1 #include<queue> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n,m,cnt,tot; 7 int st,ed; 8 int dx[8]={1,1,-1,-1,2,2,-2,-2}; 9 int dy[8]={2,-2,2,-2,1,-1,1,-1}; 10 int head[80005]; 11 int cur[80005]; 12 int dis[80005]; 13 struct node{ 14 int fr; 15 int to; 16 int nxt; 17 int flw; 18 }edge[1000005]; 19 void init(){ 20 memset(head,-1,sizeof(head)); 21 } 22 void addedge(int u,int v,int f){ 23 edge[cnt].fr=u; 24 edge[cnt].to=v; 25 edge[cnt].flw=f; 26 edge[cnt].nxt=head[u]; 27 head[u]=cnt++; 28 edge[cnt].fr=v; 29 edge[cnt].to=u; 30 edge[cnt].flw=0; 31 edge[cnt].nxt=head[v]; 32 head[v]=cnt++; 33 } 34 bool bfs(){ 35 queue<int>que; 36 memset(dis,0x3f,sizeof(dis)); 37 que.push(st);dis[st]=0; 38 while(!que.empty()){ 39 int u=que.front(); 40 que.pop(); 41 for(int i=head[u];i!=-1;i=edge[i].nxt){ 42 if(!edge[i].flw)continue; 43 int v=edge[i].to; 44 if(dis[v]==0x3f3f3f3f){ 45 dis[v]=dis[u]+1; 46 que.push(v); 47 } 48 } 49 } 50 return (dis[ed]!=0x3f3f3f3f); 51 } 52 int dfs(int u,int flw){ 53 int All=0;int tmp; 54 if(u==ed)return flw; 55 for(int i=head[u];i!=-1;i=edge[i].nxt){ 56 if(!edge[i].flw)continue; 57 int v=edge[i].to;head[u]=i; 58 if(dis[v]!=dis[u]+1)continue; 59 if((tmp=dfs(v,min(flw,edge[i].flw)))>0){ 60 flw-=tmp; 61 edge[i].flw-=tmp; 62 edge[i^1].flw+=tmp; 63 All+=tmp; 64 if(!flw)break; 65 } 66 } 67 return All; 68 } 69 int dicnic(){ 70 scanf("%d%d%d%d",&n,&m,&st,&ed); 71 int ret=0; 72 for(int i=1;i<=m;i++){ 73 int u,v,w; 74 scanf("%d%d%d",&u,&v,&w); 75 addedge(u,v,w); 76 } 77 memcpy(cur,head,sizeof(cur)); 78 while(bfs()){ 79 ret+=dfs(st,0x3f3f3f3f); 80 memcpy(head,cur,sizeof(head)); 81 } 82 return ret; 83 } 84 int main(){ 85 init(); 86 printf("%d\n",dicnic()); 87 return 0; 88 }