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. ffmpeg ffplay ffprobe资料整理

    1. 官网地址:https://ffmpeg.org/ 官网文档地址:https://ffmpeg.org/documentation.html 官网下载地址:https://ffmpeg.org/d ...

  2. 使用PHPmailer 发送邮件,使用QQ smtp服务器

    <meta charset="utf-8"> <?php include("class.phpmailer.php"); include(&q ...

  3. css运用中,对position属性的认识

    position属性有: static : 无特殊定位,对象遵循HTML定位规则 absolute : 将对象从文档流中拖出,使用left,right,top,bottom等属性进行绝对定位.而其层叠 ...

  4. [转载]七天学会NodeJS

    链接:http://nqdeng.github.io/7-days-nodejs/ NodeJS基础 什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS ...

  5. jquery在调试时出现缺少对象的错误

    1)引入的js文件出错,  检查方法:将Js的内容写在当前的页面的<script> </script>之间,看是否能够正常运行,如果不能,请核查代码  2) 如果引入的代码在当 ...

  6. cron 编辑器修改

    更改 cron 默认编辑工具 Debian 的 crontab 默认的编辑器是 nano,用起来很不习惯,怎么才能转回 VI 呢? 用如下命令即可: #update-alternatives --co ...

  7. 单片机modebus RTU通信实现,采用C语言,可适用于单片机,VC,安卓等(转)

    源:单片机modebus RTU通信实现,采用C语言,可适用于单片机,VC,安卓等 //modebus_rtu.c /***************************************** ...

  8. 【转】50条大牛C++编程开发学习建议

    每个从事C++开发的朋友相信都能给后来者一些建议,但是真正为此进行大致总结的很少.本文就给出了网上流传的对C++编程开发学习的50条建议,总结的还是相当不错的,编程学习者(不仅限于C++学习者)如果真 ...

  9. 菊花加载第三方--MBprogressHUD 分类: ios技术 2015-02-05 19:21 120人阅读 评论(0) 收藏

    上次说到了网络请求AFN,那么我们在网络请求的时候,等待期间,为了让用户不认为是卡死或程序出错,一般都会放一个菊花加载,系统有一个菊花加载类叫UIProgressHUD.但是我今天要说的是一个替代它的 ...

  10. mac解压缩

    tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)---------------.gz解压1:gunz ...