POJ1273(最大流)
Drainage Ditches
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 70451 | Accepted: 27391 |
Description
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
Output
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(最大流)的更多相关文章
- POJ1273 最大流模板
之前自己写的,以后当一个模板的基础吧,不管是最大流,最小割,二分图匹配 下面是POJ1273的一个裸题.. 1 #include <iostream> 2 #include <cst ...
- POJ1273 最大流 EK算法
套了个EK的模板 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdi ...
- POJ1273(最大流入门)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 70333 Accepted: 2733 ...
- hdu1532&&poj1273 最大流
Dinic算法: 层次图:根据源点到该点的距离建图,这里设相邻的都差1. (下面部分转) 在这幅图中我们首先要增广1->2->4->6,这时可以获得一个容量为2的流,但是如果不建立4 ...
- poj1273最大流初破
第一次网络流,学了一天的DINIC算法(个人比较愚),切了这个入门题,开始的时候怎么调连 测试都过不了,后来发现犯了一个低级错误!把判断条件放在for(:)!里面和放在for下面大大 不同啊!里面的话 ...
- poj2月下旬题解
poj2388 水题 poj1273 最大流初步 poj2456 简单的二分答案 poj2309 论lowbit的重要性 poj1734 floyd求最小环 poj1001 细节题 poj2184 0 ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- poj1273 Drainage Ditches (最大流板子
网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...
- 经典的最大流题POJ1273(网络流裸题)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
随机推荐
- spring+hibernate中的Result object returned from HibernateCallback isn't a List
Ok the problem is that for executeFind() the return type is List....so there is no way to use unique ...
- ural1126 Magnetic Storms
Magnetic Storms Time limit: 0.5 secondMemory limit: 64 MB The directory of our kindergarten decided ...
- 方法的标签_With携带
方法中参数的标签: 标签的由来:1.标签也是方法名的一部分:2.为了提高程序的阅读性:OC方法允许我们给每个参数添加一个标签来说明当前参数的含义: 标签的作用:标签是为了标识变量的,因此标签名和变量名 ...
- cocos2dx 3.2 的中国象棋游戏
改编来源:http://cn.cocos2d-x.org/tutorial/lists?id=103 在cocos2dx官网看到了这么个教程,是cocos2dx 2.x版本的,于是用 cocos2dx ...
- seajs的常用api简易文档
目前使用sea.js的公司越来越多, 比如朋友网,阿里巴巴,淘宝网,百姓网,支付宝,有道云笔记等.模块化的javascript开发带来了可维护,可扩展性,尤其在多人协作开发的时候不用再担心文件依赖和函 ...
- 前端教程&开发模块化/规范化/工程化/优化&工具/调试&值得关注的博客/Git&面试-资源汇总
内容精简 资源这么多,多看看多学习再总结肯定是好的.多读读就算看重了不算浪费时间,毕竟一千个读者就有一千个林黛玉,还有温故而知新,说不定多读一些内容,就发现惊喜了呢.不过,在此也精简一些内容,就1~2 ...
- Bitmap 格式
源:Bitmap 格式 参考:bitmap文件格式 Bitmap是Windows操作系统中的标准图像文件格式,可以分成两类:设备相关位图(DDB)和设备无关位图(DIB),DDB已经基本停用. Bit ...
- POJ 2373 Yogurt factory
简单DP. 这周所用的实际花费是上一周的花费+S与这周费用的较小值. #include<cstdio> #include<cstring> #include<cmath& ...
- ubuntu10.4的更新源因过期无法更新的解决方法
转载自:http://blog.csdn.net/suquan629/article/details/52333769 ubuntu10.4到2016年早已停止了更新支持,ubuntu也不再维护了.官 ...
- STM32 PWM波
利用STM32产生占空比可以调节的PWM波 科普:pwm(Pulse Width Modulation)脉宽调制 关于pwm波的产生:1.首先来看一下代码: pwm波模式的相关配置(利用的是定时器TI ...