本文主要是介绍poj 1273,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
入门级最大流的题,不过如果你用邻接矩阵的话,要注意有重边。
在这里,推荐一篇证明最大流最小割定理的文章:http://wenku.baidu.com/link?url=u_lLVv5Nk0XqXNcnUEKhaDupdpkYKD6IMe45PK0QafProSoW86QWQFEnz2JNKC0KdTVPz97DcwiZEFz9m85C9W-ub5bJsnp47WrNtsjGVHm。
在这道题中,我用了基于邻接矩阵的Edmond-Karp算法(参考《算法竞赛入门经典》P209),然后也用了Ford-Fulkerson算法(参考《挑战程序设计竞赛》P211)。
这两种方法思想上很类似,都是基于增广路定理,即先从源点搜索到汇点,然后增广,直到找不到增广路,此时的网络流为最大流。不过在找增广路是,前一个用BFS,后一个用DFS。
这道题是我第一次用《挑战程序设计竞赛》中的邻接表的代码,发现在poj上,要用G++来提交,用C++会CE,因为《挑战程序设计竞赛》上邻接表的代码很简洁,以至于poj的C++认为有语法问题。此外,由于邻接表是基于STL的vector,所以每次用完之后要clear,这是书上代码没有体现的。虽然这两种算法思路上不复杂,但是细节上还是很值得多多体会。
代码(C++,Edmond-Karp算法):
#include <cstdlib>
#include <iostream>
#include <queue>#define MAX 209
#define INF 2000000000
using namespace std;//#define LOCALint cap[MAX][MAX],flow[MAX][MAX],a[MAX],pre[MAX];int main(int argc, char *argv[])
{
#ifdef LOCALfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif int n,m,p,e,c,s,t,i,u,ans;queue<int> qi;while(scanf("%d %d",&n,&m)!=EOF) //n表示边数,m表示点数 {s=1; //1为源点,m为汇点t=m; memset(cap,0,sizeof(cap)); for(i=0;i<n;i++){scanf("%d %d %d",&p,&e,&c); cap[p][e]+=c; //到有重边 } memset(flow,0,sizeof(flow)); ans=0;pre[s]=s;while(1){ memset(a,-1,sizeof(a)); qi.push(s); a[s]=INF;while(!qi.empty()){u=qi.front();qi.pop();for(i=1;i<=m;i++){if(a[i]==-1&&cap[u][i]>flow[u][i]){a[i]=min(a[u],cap[u][i]-flow[u][i]);pre[i]=u; qi.push(i);} } } if(a[t]==-1) break; for(i=t;i!=s;i=pre[i]){flow[pre[i]][i]+=a[t];flow[i][pre[i]]-=a[t]; }ans+=a[t]; } printf("%d\n",ans); } system("PAUSE");return EXIT_SUCCESS;
}
代码(G++,Ford-Fulkerson算法):
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>#define MAX 209
#define INF 2000000000
using namespace std;struct Edge{int to;int c;int rev;
};vector<Edge> G[MAX];
bool vis[MAX];void add_edge(int from,int to,int cap)
{G[from].push_back((Edge){to,cap,G[to].size()});G[to].push_back((Edge){from,0,G[from].size()-1});
}int dfs(int v,int t,int f)
{int i,d;if(v==t) return f;vis[v]=true;for(i=0;i<G[v].size();i++){Edge &e=G[v][i];if(e.c>0&&!vis[e.to]){d=dfs(e.to,t,min(f,e.c));if(d>0){e.c-=d;G[e.to][e.rev].c+=d;return d;}}}return 0;
}int main()
{//freopen("in.txt","r",stdin);int n,m,p,e,c,i,s,t,ans,f;while(scanf("%d %d",&n,&m)!=EOF) //n表示边数,m表示点数{s=1; //1为源点,m为汇点t=m;for(i=0;i<n;i++){scanf("%d %d %d",&p,&e,&c);add_edge(p,e,c);}ans=0;while(1){memset(vis,false,sizeof(vis));f=dfs(s,t,INF);if(f==0) break;ans+=f;}for(i=1;i<=m;i++) G[i].clear();printf("%d\n",ans);}return 0;
}
题目( http://poj.org/problem?id=1273):
Time Limit: 1000MS | Memory Limit: 10000K | |
Description
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
这篇关于poj 1273的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!