poj 1273 Drainage Ditches

Drainage Ditches
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 54780
Accepted: 20886

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


题意:就是给出起点、终点和各个边的最大流量,求最大流。

Edmonds-Karp算法:

<span style="font-size:18px;"><pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<queue>
#define M 0x7fffffff
#define Max 506
using namespace std;

int cap[Max][Max],f[Max][Max];
int pre[Max];         //记录增广路径 
int p[Max];           //记录增广时的残量
int n,m;

int EK(int s,int t)
{
	int v,u;
	int sum=0;
	queue<int >q;
	memset(f,0,sizeof(f));
	while(true)            //BFS找增广路
	{
		memset(p,0,sizeof(p));
		p[s]=M;
		q.push(s);
		while(!q.empty())
		{
			u=q.front();
			q.pop();
			for(v=1;v<=m;v++)
			{
				if(!p[v] && f[u][v]<cap[u][v])        //满足条件表示找到新节点 
					                                  //f[a][b]表示a到b的流量,cap[a][b]表示a到b的容量. 
				{
					pre[v]=u;           //记录v的父亲,并加入队列
					q.push(v);
					p[v]=p[u]<cap[u][v]-f[u][v]?p[u]:cap[u][v]-f[u][v];   // s-v路径上的最小残量 
				}
			}
		}
		if(p[t]==0) break;        //找不到增广路,则当前流已经是最大流(最小割最大流定理)
		for(u=t;u!=s;u=pre[u])    //依次(从t开始)更新s-v路径上的流量值
		{
			f[pre[u]][u]+=p[t];      //更新正向流量 
			f[u][pre[u]]-=p[t];      //更新反向流量 
		}
		sum+=p[t];     //更新从s流出的总流量  
	}
	return sum;
}

int main ()
{
	int i;
	int a,b,c;
	while (~scanf("%d%d",&n,&m))   // n条路径,m个点.
	{
		memset(cap,0,sizeof(cap));
		for(i=0;i<n;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			cap[a][b]+=c;        //可能输入相同的边. cap[a][b]表示a到b的容量值
		}
		printf("%d\n",EK(1,m));   
	}
	return 0;
}
</span>


 
Ford-Fulkerson算法: 

#include <iostream>  
#include <queue>  
using namespace std;  
#define maxN 201  
int edge[maxN][maxN];  
int visited[maxN];  //标记
int father[maxN];   
int N, M;         //边数,顶点数  
int ans;       
void Ford_Fulkerson( )  
{  
    while(1)       //一次大循环,找到一条可能的增广路径 
    {  
        queue <int> q;  
        memset(visited, 0, sizeof(visited));  
        memset(father, -1, sizeof(father));  
       
        visited[1] = 1;  
        q.push(1);  
        while(!q.empty())   //广度优先搜索  
        {  
            int now = q.front();  
            q.pop();  
            if(now == M) break;  
            for(int i=1;i<=M;i++)  //每次父亲节点都要更新,权值减为0的边就不算了.
            {  
                if(edge[now][i] && !visited[i])  
                {  
                    father[i] = now;  
                    visited[i] = 1;  
                    q.push(i);  
                }  
            }  
        }  
       
        if(!visited[M])   break;   //可能的增广路不存在了  
        int u, min = 0xFFFF;  
        for(u = M; u!=1; u = father[u])    //找出权值最小的边  
        {  
            if(edge[father[u]][u] < min)  
                min = edge[father[u]][u];  
        }  
        
        for(u = M; u!=1; u = father[u])    
        {               
            edge[father[u]][u] -= min;  //前向弧减去 
            edge[u][father[u]] += min;  //后向弧加上
        }  
        ans += min;  //当前增广路径增加的流 
    }  
}  

int main()  
{  
    int s, e, w;  
    while(cin >> N >> M)  
    {  
        ans = 0;  
        memset(edge, 0, sizeof(edge));  
        for(int i = 0; i<N; i++)  
        {  
            cin >> s >> e >> w;  
            edge[s][e] += w;  
        }  
        Ford_Fulkerson();  
        cout << ans << endl;  
    }  
    return 0;  
}  

Dinic算法:(此算法还待理解。。。)

<span style="font-size:18px;">#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define min(a,b)(a<b?a:b)
#define INF 1000000
#define MAX 210

struct Node
{
    int cap;
    int flow;
}map[MAX][MAX];

int sx,ex;       //sx和ex分别代表源点和汇点
int pre[MAX];
int n,m;

bool BFS()        //BFS搜索层次网络
{
    memset(pre,0,sizeof(pre));
    queue<int > Q;
    Q.push(sx);
    pre[sx]=1;
    while(!Q.empty())
    {
        int d=Q.front();
        Q.pop();
        for(int i=1;i<=n;i++)
        {
            if(!pre[i] && map[d][i].cap-map[d][i].flow)
            {
                pre[i]=pre[d]+1;
                Q.push(i);
            }
        }
    }
	if(pre[ex]!=0)
        return 1;
	else 
		return 0;
}

int Dinic(int pos,int flow)   //pos是顶点号,flow是当前顶点所能得到的流量
{
    int f=flow;

    if(pos==ex)
      return flow;

    for(int i=1;i<=n;i++)
    {
        if(map[pos][i].cap-map[pos][i].flow && pre[pos]+1==pre[i])
        {
            int a=map[pos][i].cap-map[pos][i].flow;
            int t=Dinic(i,min(a,flow));

            map[pos][i].flow+=t;
            map[i][pos].flow-=t;
            flow-=t;  //???
        }
    }
    return f-flow;  // ???
}


int slove()
{
    int sum=0;
    while(BFS())
    {
        sum+=Dinic(sx,INF);
    }
    return sum;
}

int main()
{
    int u,v,w;
    while(cin>>m>>n)
    {
        sx=1;
        ex=n;
       memset(map,0,sizeof(map)) ;
       for(int i=1;i<=m;i++)
       {
           cin>>u>>v>>w;
           map[u][v].cap+=w;
       }
       cout<<slove()<<endl;
    }
    return 0;
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值