Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 70451   Accepted: 27391

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。
 //2016.9.22
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 205 using namespace std; const int inf = 0x3f3f3f3f; struct Edge
{
int from, to , cap, flow;//分别为边的两个端点、容量、边上的流
Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f){}
}; struct EdmondsKarp//Edmonds-Karp算法
{
int n, m;//结点编号0——n-1
vector<Edge> edges;//存边与反向弧
vector<int> G[N];//邻接表,G[i][j]表示结点i的第j条边在数组edges中的序号
int a[N];//当起点到i的可改进量
int p[N];//最短路树上p的入弧编号 void init(int n)//初始化
{
for(int i = ; i < n; i++)G[i].clear();
edges.clear();
} void addEdge(int from, int to, int cap)//添加边
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));//反向弧
m = edges.size();
G[from].push_back(m-);//加入编号
G[to].push_back(m-);
} int maxFlow(int s, int t)//最大流,s为源点,t为汇点
{
int flow = ;
while()
{
memset(a, , sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = inf;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if(!a[e.to] && e.cap>e.flow)
{
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t])break;
}
if(!a[t])break;//残余网络中不存在增广路,当前流是最大流
for(int u = t; u != s; u = edges[p[u]].from)//找到一条增广路
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];//p[u]^1为p[u]的反向边
}
flow += a[t];
}
return flow;
}
}; int main()
{
int n, m;
EdmondsKarp e;
while(scanf("%d%d", &m, &n)!=EOF)
{
int u, v, c;
e.init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &c);
u--, v--;
e.addEdge(u, v, c);
}
printf("%d\n", e.maxFlow(, n-));
} return ;
}

POJ1273(最大流)的更多相关文章

  1. POJ1273 最大流模板

    之前自己写的,以后当一个模板的基础吧,不管是最大流,最小割,二分图匹配 下面是POJ1273的一个裸题.. 1 #include <iostream> 2 #include <cst ...

  2. POJ1273 最大流 EK算法

    套了个EK的模板 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdi ...

  3. POJ1273(最大流入门)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 70333   Accepted: 2733 ...

  4. hdu1532&&poj1273 最大流

    Dinic算法: 层次图:根据源点到该点的距离建图,这里设相邻的都差1. (下面部分转) 在这幅图中我们首先要增广1->2->4->6,这时可以获得一个容量为2的流,但是如果不建立4 ...

  5. poj1273最大流初破

    第一次网络流,学了一天的DINIC算法(个人比较愚),切了这个入门题,开始的时候怎么调连 测试都过不了,后来发现犯了一个低级错误!把判断条件放在for(:)!里面和放在for下面大大 不同啊!里面的话 ...

  6. poj2月下旬题解

    poj2388 水题 poj1273 最大流初步 poj2456 简单的二分答案 poj2309 论lowbit的重要性 poj1734 floyd求最小环 poj1001 细节题 poj2184 0 ...

  7. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  8. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...

  9. 经典的最大流题POJ1273(网络流裸题)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

随机推荐

  1. Linux下VNC配置多个桌面和修改密码 不会当系统重启vnc失效

    1:vncserver 2:iptables -I INPUT -p tcp --dport 5901 -j ACCEPT   客户端方式 3:iptables -I INPUT -p tcp --d ...

  2. PAT (Advanced Level) 1024. Palindromic Number (25)

    手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...

  3. iframe载入等待

    <style> #pageloading{position:absolute; left:0px; top:0px;background:white url('../images/load ...

  4. jQuery.extend({...})分析

    作者:zccst 看一下是如何写的 jQuery.extend({ prop:"" method:function(){} }); 可以看出,这些方法是jQuery的静态属性和方法 ...

  5. winscp 秘钥登录

    如题 如果不想用密码登录,可以选择用秘钥文件登录winscp 原理和linux分发ssh公钥是一个道理 1:在被管理的机器上分发公钥 ,出现 authorized_keys才可以 完成服务端配置 2: ...

  6. oracle 11g odbc连接串及配置

    首先先安装HA-Instant Client-v11.2.0.3.0-x86.rar 下载地址: ftp://hhdown:2-2@58.23.131.52/download/HA-Instant%2 ...

  7. 小偷网站工具--Teleport Ultra

    可以克隆别人网站的工具 http://jingyan.baidu.com/article/219f4bf7dce58bde442d3836.html

  8. STM32驱动W25X64存储器

    W25X64 是华邦公司推出的大容量 SPI  FLASH 产品,W25X64 的容量为 64Mbit(8M),该系列还有 W25Q80/16/32 等.W25X16,W25X32,W25X64分别有 ...

  9. 友坚恒天.开发板(Cotex-A9 Exynos4412 开发板)

    友坚恒天.开发板 Cotex-A9 Exynos4412 开发板

  10. iOS获取设备唯一标识的各种方法?IDFA、IDFV、UDID分别是什么含义?

    一.UDID (Unique Device Identifier) UDID的全称是Unique Device Identifier,顾名思义,它就是苹果IOS设备的唯一识别码,它由40个字符的字母和 ...