标签:
题目链接:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 67475 | Accepted: 26075 |
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
题意:
就是求一个1到n的最大流,入门题;
思路:
简直就是模板,有个wa点就是有重边;
AC代码:
//#include <bits/stdc++.h> #include <iostream> #include <queue> #include <cmath> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=2e4+5; int n,m,flow[250],path[250],cap[250][250]; queue<int>qu; /* struct Edge { int to,next,val; }edge[4*N]; void add_edge(int s,int e,int val) { edge[cnt].to=e; edge[cnt].next=head[s]; edge[cnt].val=val; head[s]=cnt++; } queue<int>qu; void spfa() { while(!qu.empty())qu.pop(); mst(vis,0); mst(dis,inf); mst(num,0); qu.push(1); dis[1]=0; vis[1]=1; while(!qu.empty()) { int fr=qu.front(); qu.pop(); num[fr]++; if(num[fr]>n) { printf("-1\n"); return ; } for(int i=head[fr];i!=-1;i=edge[i].next) { int x=edge[i].to; if(dis[x]>dis[fr]+edge[i].val) { dis[x]=dis[fr]+edge[i].val; if(!vis[x]) { qu.push(x); vis[x]=1; } } } vis[fr]=0; } if(dis[n]==inf)printf("-2\n"); else printf("%d\n",dis[n]); }*/ int bfs() { while(!qu.empty())qu.pop(); mst(path,-1); path[1]=0; flow[1]=inf; qu.push(1); while(!qu.empty()) { int fr=qu.front(); qu.pop(); Riep(m) { if(i!=1&&cap[fr][i]&&path[i]==-1) { path[i]=fr; flow[i]=min(cap[fr][i],flow[fr]); qu.push(i); } } } if(path[m]==-1)return -1; return flow[m]; } int maxflow() { int sum=0; int temp,now,pre; while(1) { temp=bfs(); if(temp==-1)break; sum+=temp; now=m; while(now!=1) { pre=path[now]; cap[pre][now]-=temp; cap[now][pre]+=temp; now=pre; } } return sum; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int u,v,w; mst(cap,0); Riep(n) { scanf("%d%d%d",&u,&v,&w); cap[u][v]+=w; } printf("%d\n",maxflow()); } return 0; }
poj-1273 Drainage Ditches(最大流基础题)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5479702.html