http://poj.org/problem?id=1273

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 62708   Accepted: 24150

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

题目大意:m条边,每条边都有一个流量值,n个点,求1到n的最大流量

Dinic模板:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#define N 210
#define INF 0x3f3f3f3f using namespace std; int G[N][N], vis[N], layer[N];
int n; bool BFS()//分层处理
{
deque<int>Q;//定义双端队列
memset(layer, -, sizeof(layer));
Q.push_back();//源点入队列(双端队列尾部增加一个元素x)
layer[] = ;//标记源点
while(!Q.empty())
{
int u = Q.front(), i;
Q.pop_front();//删除双端队列中最前一个元素
for(i = ; i <= n ; i++)//遍历点判断是否能分层
{
if(G[u][i] > && layer[i] == -)//当u点到i点这条边有流量且i点尚未被分层
{
layer[i] = layer[u] + ;//则将i点分层
if(i == n)//当到达汇点时分层成功
return true;
else
Q.push_back(i);//否则继续
}
}
}
return false;
} int Dinic()
{
int Maxflow = ;
while(BFS() == true)
{
deque<int>Q;
memset(vis, , sizeof(vis));
Q.push_back();
vis[] = ;
while(!Q.empty())
{
int v = Q.back(), i;
if(v != n)
{
for(i = ; i <= n ; i++)
{
if(G[v][i] > && layer[v] + == layer[i] && !vis[i])//如果v到i有流量且i点是v点的增广路且点i未被访问过
{
vis[i] = ;
Q.push_back((i));//点i进入队列(即点i属于增广路上的一个点)
break;
}
}
if(i > n)//如果遍历所有点后在下一层没有找到增广路,就退出本层继续寻找下一条
Q.pop_back();//删除双端队列中最后一个元素
}//找增广路
else
{
int Minflow = INF, nv;
int len = Q.size();//进入队列中点的个数
for(int i = ; i < len ; i++)
{
int x = Q[i - ];//前一个点
int y = Q[i];//后一个点
if(Minflow > G[x][y])
{
Minflow = G[x][y];
nv = x;//nv记录前端点
}//查找最小值即增光流量
}
Maxflow += Minflow;
for(int i = ; i < len ; i++) //更新流量
{
int x = Q[i - ];
int y = Q[i];
G[x][y] -= Minflow;//更新正向
G[y][x] += Minflow;//反向增加
}
while(!Q.empty() && Q.back() != nv)
Q.pop_back();//出队
}
}
}
return Maxflow;
} int main()
{
int a, b, c, m;
while(~scanf("%d%d", &m, &n))
{
memset(G, , sizeof(G));
while(m--)
{
scanf("%d%d%d", &a, &b, &c);
G[a][b] += c;//处理重边
}
printf("%d\n", Dinic());
}
return ;
}
/*
10 8
1 2 10
1 3 10
2 4 20
2 5 20
3 6 20
3 7 20
4 8 30
5 8 30
6 8 30
7 8 30
*/

邻接表+Dinic(仅供参考)

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#define INF 0x3f3f3f3f
#define N 1010
using namespace std; struct Edge
{
int u, v, next, flow;
} edge[N * N]; int layer[N], head[N], cnt; void Init()
{
memset(head, -, sizeof(head));
cnt = ;
} void AddEdge(int u, int v, int flow)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].flow = flow;
edge[cnt].next = head[u];
head[u] = cnt++;
} bool BFS(int Start, int End)//分层
{
queue<int>Q;
memset(layer, -, sizeof(layer));
Q.push(Start);
layer[Start] = ;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(u == End)
return true;
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if(layer[v] == - && edge[i].flow > )
{
layer[v] = layer[u] + ;
Q.push(v);
}
}
}
return false;
} int DFS(int u, int Maxflow , int End)
{
if(u == End)
return Maxflow;
int uflow = ;
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if(layer[v] == layer[u] + && edge[i].flow > )
{
int flow = min(edge[i].flow, Maxflow - uflow);
flow = DFS(v, flow, End);
edge[i].flow -= flow;
edge[i^].flow += flow;
uflow += flow; if(uflow == Maxflow)
break;
}
}
if(uflow == )
layer[u] = ;
return uflow;
} int Dinic(int Start, int End)
{
int Maxflow = ;
while(BFS(Start, End))
Maxflow += DFS(Start, INF, End);
return Maxflow;
} int main()
{
int m, n;
while(~scanf("%d%d", &m, &n))
{
Init();
int u, v, flow;
while(m--)
{
scanf("%d%d%d", &u, &v, &flow);
AddEdge(u, v, flow);
AddEdge(v, u, );
}
printf("%d\n", Dinic(, n));
}
return ;
}

poj 1273 Drainage Ditches(最大流)的更多相关文章

  1. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  2. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  3. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  4. POJ 1273 Drainage Ditches 最大流

    这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...

  5. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  6. POJ 1273 Drainage Ditches(最大流Dinic 模板)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  7. POJ 1273 Drainage Ditches (网络最大流)

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

  8. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

  9. POJ 1273 Drainage Ditches(网络流,最大流)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

随机推荐

  1. 【第六篇】javascript显示当前的时间(年月日 时分秒 星期)

    不多说自己上代码 这是我开始学javascript写的,现在发出来 <span id="clock" ></span> function time() { ...

  2. Windows pyqt4 bat自动转换UI文件-->.pyw文件

    /***************************************************************************** * Windows pyqt4 bat自动 ...

  3. (转)每天一个Linux命令(5): rm

    http://www.cnblogs.com/peida/archive/2012/10/26/2740521.html 昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和 ...

  4. android 相对布局

    RelativeLayout布局 android:layout_marginTop="25dip" //顶部距离 android:gravity="left" ...

  5. 学习macos常用的一些快捷键笔记

    学习mac 操作系统使用笔记 Dock功能学习 类似快捷图标一样 Command+q quit a program Dock上添加与删除都用拖动 command+delete 删除文件 shift+c ...

  6. Linux多线程下载工具Axel

    一般情况,在 Linux 下我们习惯于用 wget 下载,但该工具的缺点就是无法进行多线程下载,所以往往有时候速度不够快.这里介绍的 Axel,是 Linux 下一款不错的 HTTP 或 FTP 高速 ...

  7. Android下EditText的hint的一种显示效果------FloatLabelLayout

    效果: 此为EditText的一种细节,平时可能用的不多,但是用户体验蛮好的,特别是当注册页面的项目很多的时候,加上这种效果,体验更好 仅以此记录,仅供学习参考. 参考地址:https://gist. ...

  8. nodejs 改变全局前缀

    npm的包安装分为本地安装(local).全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如: 复制代码 代码如下: npm install grunt # 本地安装npm ...

  9. 嵌入式 Linux线程同步读写锁rwlock示例

    读写锁比mutex有更高的适用性,可以多个线程同时占用读模式的读写锁,但是只能一个线程占用写模式的读写锁.1. 当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞:2. ...

  10. JDBC用ResultSet访问大量数据时会遇到的问题

    我们经常需要JDBC来对数据库就行操作,一般流程为连接数据库.通过sql语句把需要的数据取出来保存到ResultSet,然后调用ResultSet方法的类似 getString,getInt()等方法 ...