zoukankan      html  css  js  c++  java
  • poj_1273Drainage Ditches

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 47664   Accepted: 17948

    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

    网络流第一题,参考刘汝佳的白书

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 250;
    const int INF = 0x7FFFFFFF;
    int flow[MAXN][MAXN], cap[MAXN][MAXN];
    int maxflow, n, m;
    
    void EKarp(int s, int e)
    {
    	queue<int>Q;
    	int u, v, a[MAXN], father[MAXN];
    	maxflow = 0;
    	memset(flow, 0, sizeof(flow));
    	while (true)
    	{
    		memset(a, 0, sizeof(a));
    		a[s] = INF;
    		Q.push(s);
    		while (!Q.empty())//BFS找增广路
    		{
    			int u = Q.front();
    			Q.pop();
    			for (v = 1; v <= n; v++)
    			{
    				if(a[v] == 0 && cap[u][v] > flow[u][v])//找到新节点v
    				{
    					father[v] = u;//记录v的父亲,并加入FIFO队列
    					Q.push(v);
    					a[v] = min(a[u], cap[u][v] - flow[u][v]);//s-v路径上的最小残量
    				}
    			}
    		}
    		if(a[e] == 0)//找不到,则当前流已经是最大流
    		{
    			break;
    		}
    		for (u = e; u != s; u = father[u])//从汇点往回走
    		{
    			flow[father[u]][u] += a[e];//更新正向流量
    			flow[u][father[u]] -= a[e];//更新反向流量
    		}
    		maxflow += a[e];//更新从s流出的总流量
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int x, y, z;
    	while(scanf("%d %d", &m, &n) != EOF)
    	{
    		memset(cap, 0, sizeof(cap));
    		while (m--)
    		{
    			scanf("%d %d %d", &x, &y, &z);
    			cap[x][y] += z;
    		}
    		EKarp(1, n);
    		printf("%d\n", maxflow);
    	}
    	return 0;
    }


  • 相关阅读:
    Oracle查看正在执行的存储过程的sid---转
    使用WITH子句重用子查询
    oracle解决显示数据的层次问题--实现数据缩进
    oracle9i、10g、11g区别 --转
    oracle10g安装在裸设备上
    在Linux系统上面创建使用裸设备的数据库
    监控Oracle数据库的常用shell脚本-转
    sql server使用维护计划定时备份完整数据库、差异数据库
    使用Advanced Installer14.3 简单打包windows窗体应用程序
    SVG Path标签 A 参数
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834997.html
Copyright © 2011-2022 走看看