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;
    }


  • 相关阅读:
    js+jq 淡入淡出轮播(点击+定时+鼠标进入移出事件)
    DBCA静默方式建库
    Oracle 11g常用管理命令(用户、表空间、权限)
    RedHat 7.3 Oracle 12.2.0.1 RAC 安装手册(转)
    rsyslog和logrotate服务
    Linux 之 rsyslog 系统日志转发
    Flatty Shadow图标自动产生器——在线生成各种扁平化 ICON
    助力前端开发——20个精心挑选新鲜好用的前端开发工具
    15款新鲜出炉的实用网页设计工具
    提供免费可商用的优秀背景视频素材——COVERR
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834997.html
Copyright © 2011-2022 走看看