您的位置:首页 > 理论基础 > 计算机网络

poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

2014-05-03 17:51 597 查看
Drainage Ditches

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 53640Accepted: 20448
Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
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

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

Source

USACO 93

n是点数,m是边数

EK算法:
O(n*m*m)
效率最低,没有任何优化,直接吧标号法实现。
算法思想:一直重复搜索增广路径的过程,知道不存在增广路径。而此处对于增广路径的搜索,则采用了邻接矩阵的方法,耗时比较大,使用邻接表会更清晰的表现出标号法的过程。

//15MS    320K    1811 B    G++
#include<iostream>
#define N 1005
#define inf 0x7fffffff
using namespace std;
struct node{
int v;
int c;
int next;
}edge
;

int head
,eid;
int n,m;

int h
;  //距离标号
int gap
; //记录是否断层

void addedge(int u,int v,int c) //有向图
{
edge[eid].v=v;
edge[eid].c=c;
edge[eid].next=head[u];
head[u]=eid++;
edge[eid].v=u;
edge[eid].c=0;
//edge[eid].c=c; //有向图
edge[eid].next=head[v];
head[v]=eid++;
}
int dfs(int u,int tc)
{
if(u==n) return tc; //找到增广路径
int c0;
int minh=n-1;
int temp=tc;
for(int i=head[u];i!=-1;i=edge[i].next){ //遍历与u有关的弧
int v=edge[i].v;
int c=edge[i].c;
if(c>0){
if(h[v]+1==h[u]){ //可行弧
c0=temp<c?temp:c;
c0=dfs(v,c0);
edge[i].c-=c0;
edge[i^1].c+=c0;
temp-=c0;
if(h[1]>=n) return tc-temp;
if(temp==0) break;
}
if(h[v]<minh) minh=h[v]; //更新最短距离标号
}
}
if(temp==tc){ //不存在可行弧,回退操作
--gap[h[u]];
if(gap[h[u]]==0) h[1]=n; //出现断层
h[u]=minh+1;
++gap[h[u]];
}
return tc-temp;
}
int ISAP() //isap算法
{
int ans=0;
memset(gap,0,sizeof(gap));
memset(h,0,sizeof(h));
gap[1]=n;
while(h[1]<n){
ans+=dfs(1,inf);
}
return ans;
}
int main(void)
{
int u,v,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(head,-1,sizeof(head));
eid=0;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
}
printf("%d\n",ISAP());
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: