标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10242 Accepted Submission(s): 4867
#include<iostream> #include<cstdio> #include<cstring> #include<cstring> #include<vector> #include<queue> #include<algorithm> using namespace std; const int inf=0x3fffffff; const int maxn=207; struct Edge { int cap,flow; }; int n,m,s,t; Edge edges[maxn][maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void init() { memset(edges,0,sizeof(edges)); } bool BFS() { memset(vis,0,sizeof(vis)); queue<int>Q; Q.push(s); d[s]=0; vis[s]=1; while(!Q.empty()){ int x=Q.front();Q.pop(); for(int i=1;i<=n;i++){ Edge &e=edges[x][i]; if(!vis[i]&&e.cap>e.flow){ vis[i]=1; d[i]=d[x]+1; Q.push(i); } } } return vis[t]; } int DFS(int u,int cp)//进行增广 { int tmp=cp; int v,t; if(u==n) return cp; for(v=1;v<=n&&tmp;v++) { if(d[u]+1==d[v]) { if(edges[u][v].cap>edges[u][v].flow) { t=DFS(v,min(tmp,edges[u][v].cap-edges[u][v].flow)); edges[u][v].flow+=t; edges[v][u].flow-=t; tmp-=t; } } } return cp-tmp; } int Maxflow() { int flow=0; while(BFS()){ memset(cur,0,sizeof(cur)); flow+=DFS(s,inf); } return flow; } int main() { //freopen("in.txt","r",stdin); int a,b, c; while(~scanf("%d%d",&m,&n)){ init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); edges[a][b].cap+=c; } s=1;t=n; int ans=Maxflow(); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/codeyuan/p/4262738.html