Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16564 Accepted Submission(s):
7867
#include <cstring> #include <cstdio> #include <queue> using namespace std; struct node { int next,to,flow; }edge[200*200+1]; int deep[200*200],head[200*200+1],ans,s,t,n,m,i,j,cnt=1; void add(int u,int v,int l) { cnt++; node * now=&edge[cnt]; now->next=head[u]; now->to=v; now->flow=l; head[u]=cnt; } bool bfs(int s,int t) { memset(deep,-1,sizeof(deep)); deep[s]=0; queue<int>q; q.push(s); while(!q.empty()) { int tp=q.front(); q.pop(); for(i=head[tp];i;i=edge[i].next) { if(deep[edge[i].to]==-1&&edge[i].flow>0) { deep[edge[i].to]=deep[tp]+1; if(edge[i].to==t) return 1; else q.push(edge[i].to); } } } return 0; } int dfs(int now,int t,int came_flow) { if(now==t||came_flow==0) return came_flow; int rest=0,f; for(int i=head[now];i;i=edge[i].next) { int v=edge[i].to; if(deep[v]==deep[now]+1&&edge[i].flow>0) { f=dfs(v,t,min(came_flow,edge[i].flow)); if(f) { rest+=f; came_flow-=f; edge[i].flow-=f; edge[i^1].flow+=f; } if(came_flow==0) return rest; } } return rest; } void Dinic(int s,int t) { while(bfs(s,t)) ans+=dfs(s,t,1e9); } int main() { while(scanf("%d%d",&n,&m)!=EOF)//一定用这个读入,因为这WA了好多次 { int a,b,c; s=1,t=m;ans=0; memset(head,0,sizeof(head)); cnt=1; while(n--) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,0); } Dinic(s,t); printf("%d\n",ans); } return 0; }
原文:http://www.cnblogs.com/ruojisun/p/6539545.html