标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3368 Accepted Submission(s): 1001
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int INF = 999999999; const int N = 1005; const int M = 200005; struct Edge{ int v,w,next; }edge[M]; int head[N]; int level[N]; int tot; void init() { memset(head,-1,sizeof(head)); tot=0; } void addEdge(int u,int v,int w,int &k) { edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++; edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++; } int BFS(int src,int des) { queue<int>q; memset(level,0,sizeof(level)); level[src]=1; q.push(src); while(!q.empty()) { int u = q.front(); q.pop(); if(u==des) return 1; for(int k = head[u]; k!=-1; k=edge[k].next) { int v = edge[k].v; int w = edge[k].w; if(level[v]==0&&w!=0) { level[v]=level[u]+1; q.push(v); } } } return -1; } int dfs(int u,int des,int increaseRoad){ if(u==des||increaseRoad==0) { return increaseRoad; } int ret=0; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w=edge[k].w; if(level[v]==level[u]+1&&w!=0){ int MIN = min(increaseRoad-ret,w); w = dfs(v,des,MIN); if(w > 0) { edge[k].w -=w; edge[k^1].w+=w; ret+=w; if(ret==increaseRoad){ return ret; } } else level[v] = -1; if(increaseRoad==0) break; } } if(ret==0) level[u]=-1; return ret; } int Dinic(int src,int des) { int ans = 0; while(BFS(src,des)!=-1) ans+=dfs(src,des,INF); return ans; } struct Edge1{ int v,w,next; }edge1[M],edge2[M]; int head1[N],head2[N]; int tot1,tot2; int n,m,a,b; void addEdge1(int u,int v,int w,int &k){ edge1[k].v = v,edge1[k].w=w,edge1[k].next = head1[u],head1[u]=k++; } void addEdge2(int u,int v,int w,int &k){ edge2[k].v = v,edge2[k].w=w,edge2[k].next = head2[u],head2[u]=k++; } void init1(){ memset(head1,-1,sizeof(head1)); tot1 = 0; } void init2(){ memset(head2,-1,sizeof(head2)); tot2 = 0; } int low[2][N]; bool vis[N]; void spfa(int s,int t,int flag,int *head,Edge1 edge[]){ for(int i=1;i<=n;i++){ low[flag][i] = INF; vis[i] = false; } low[flag][s] = 0; queue<int >q; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = false; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w=edge[k].w; if(low[flag][v]>low[flag][u]+w){ low[flag][v] = low[flag][u]+w; if(!vis[v]){ vis[v] = true; q.push(v); } } } } } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ init1(); init2(); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); if(u==v) continue; addEdge1(u,v,w,tot1); addEdge2(v,u,w,tot2); ///反向边 } scanf("%d%d",&a,&b); spfa(a,b,0,head1,edge1); ///a->b 第一遍 spfa(b,a,1,head2,edge2); ///b->a 第二遍 init(); for(int i=1;i<=n;i++){ for(int j=head1[i];j!=-1;j=edge1[j].next){ int v = edge1[j].v,w=edge1[j].w; if(low[0][i]+low[1][v]+w==low[0][b]){ addEdge(i,v,1,tot); } } } if(low[0][b]==INF){ printf("0\n"); continue; } int max_flow = Dinic(a,b); printf("%d\n",max_flow); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5727033.html