图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 91585 | Accepted: 35493 |
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
Source
因为这个题要考虑吧,多次对一条边增加流量,所以要用邻接矩阵来处理。这里给出两个代码,当前弧优化,和非当前弧优化版。
#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int tab[250][250];//邻接矩阵
int dis[250];//距源点距离,分层图
int cur[280]; //当前弧优化
int N,M;//N:点数;M,边数
queue<int> Q;
int BFS()
{
memset(dis,0xff,sizeof(dis));//以-1填充
dis[1]=0;
Q.push(1);
while (Q.size())
{
int head=Q.front();
Q.pop();
for (int i=1; i<=N; i++)
if (dis[i]<0 && tab[head][i]>0)
{
dis[i]=dis[head]+1;
Q.push(i);
}
}
if (dis[N]>0) return 1;
else return 0;//汇点的DIS小于零,表明BFS不到汇点
}
//dfs代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int dfs(int x,int low)//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
{
int a=0;
if (x==N)
return low;//是汇点
for (int &i=cur[x]; i<=N; i++)
if (tab[x][i] >0 //联通
&& dis[i]==dis[x]+1 //是分层图的下一层
&&(a=dfs(i,min(low,tab[x][i]))))//能到汇点(a != 0)
{
tab[x][i]-=a;
tab[i][x]+=a;
return a;
}
return 0;
}
int dinic()
{
int ans=0,tans;
while (BFS())//要不停地建立分层图,如果BFS不到汇点才结束
{
for(int i=1;i<=N;i++)
cur[i]=1;
while(tans=dfs(1,0x7fffffff))ans+=tans;//一次BFS要不停地找增广路,直到找不到为止
}
return ans;
}
int main()
{
int i,j,f,t,flow,tans;
while (scanf("%d%d",&M,&N)!=EOF)
{
memset(tab,0,sizeof(tab));
for (i=1; i<=M; i++)
{
scanf("%d%d%d",&f,&t,&flow);
tab[f][t]+=flow;
}
printf("%d\n",dinic());
}
}
#include <iostream>
#include <cstdio>
#include <math.h>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int tab[250][250];//邻接矩阵
int dis[250];//距源点距离,分层图
int N,M;//N:点数;M,边数
queue<int> Q;
int BFS()
{
memset(dis,0xff,sizeof(dis));//以-1填充
dis[1]=0;
Q.push(1);
while (Q.size())
{
int head=Q.front();
Q.pop();
for (int i=1; i<=N; i++)
if (dis[i]<0 && tab[head][i]>0)
{
dis[i]=dis[head]+1;
Q.push(i);
}
}
if (dis[N]>0) return 1;
else return 0;//汇点的DIS小于零,表明BFS不到汇点
}
//dfs代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int dfs(int x,int low)//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
{
int a=0;
if (x==N)
return low;//是汇点
for (int i=1; i<=N; i++)
if (tab[x][i] >0 //联通
&& dis[i]==dis[x]+1 //是分层图的下一层
&&(a=dfs(i,min(low,tab[x][i]))))//能到汇点(a != 0)
{
tab[x][i]-=a;
tab[i][x]+=a;
return a;
}
return 0;
}
int dinic()
{
int ans=0,tans;
while (BFS())//要不停地建立分层图,如果BFS不到汇点才结束
{
while(tans=dfs(1,0x7fffffff))ans+=tans;//一次BFS要不停地找增广路,直到找不到为止
}
return ans;
}
int main()
{
int i,j,f,t,flow,tans;
while (scanf("%d%d",&M,&N)!=EOF)
{
memset(tab,0,sizeof(tab));
for (i=1; i<=M; i++)
{
scanf("%d%d%d",&f,&t,&flow);
tab[f][t]+=flow;
}
printf("%d\n",dinic());
}
}
图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)的更多相关文章
- 网络流 最大流 Drainage Ditches Dinic
hdu 1532 题目大意: 就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速,本题就是让你求出最大流速,无疑要运用到求最大流了 ...
- 【uva 11082】Matrix Decompressing(图论--网络流最大流 Dinic+拆点二分图匹配)
题意:有一个N行M列的正整数矩阵,输入N个前1~N行所有元素之和,以及M个前1~M列所有元素之和.要求找一个满足这些条件,并且矩阵中的元素都是1~20之间的正整数的矩阵.输入保证有解,而且1≤N,M≤ ...
- 【uva 753】A Plug for UNIX(图论--网络流最大流 Dinic)
题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就 ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
- 图论--网络流--最大流 HDU 2883 kebab(离散化)
Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled ...
- 图论--网络流--最大流--POJ 1698 Alice's Chance
Description Alice, a charming girl, have been dreaming of being a movie star for long. Her chances w ...
- 图论--网络流--最大流 POJ 2289 Jamie's Contact Groups (二分+限流建图)
Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very ...
- 图论--网络流--最大流 洛谷P4722(hlpp)
题目描述 给定 nn 个点,mm 条有向边,给定每条边的容量,求从点 ss 到点 tt 的最大流. 输入格式 第一行包含四个正整数nn.mm.ss.tt,用空格分隔,分别表示点的个数.有向边的个数.源 ...
- 图论--网络流--费用流POJ 2195 Going Home
Description On a grid map there are n little men and n houses. In each unit time, every little man c ...
随机推荐
- 按公式产生随机数、java中的重载、递归、有关计算机计算的问题
1.按公式产生随机数x1=(16807*x)%(Integer.MAX_VALUE)x=x1;通过这个公式进行随机数的产生,当产生的数字大于2e+32-2时,在用产生随机数的方式进行数字的输出.主要思 ...
- MTK Android Android数据保存到系统数据库
如果有留意Android中系统设置Settings里面的源码,你会发现代码中频繁用到了Settings.System操作,该类通过键值对的形式,将一些特定的值以全局的模式保存到Setting的数据库中 ...
- ubuntu18.04配置宽带上网
1.将 /etc/NetworkManager 目录下的 managed标签改为true 2.将 /etc/network/ 目录下的 interfaces文件只留下前两行: auto lo ifac ...
- 2017蓝桥杯日期问题(C++B组)
标题:日期问题小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都在1960年1月1日至2059年12月31日.令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的, ...
- 分治算法(C++版)
#include<iostream>using namespace std; void printArray(int array[],int length) { for (i ...
- C语言移动一个点
#include"stdio.h"#include"windows.h"#include"conio.h"#define M 3#defin ...
- javascript 入门 之select2获取远程数据
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta lan ...
- Linux 磁盘管理篇, 内存交换空间
swap是在系统内存不足的情况下,以硬盘暂时来储存内存中的一些数据来继续程序的执行 查看内存使用情况 free 格式化为swap格式 mkswap 启动sw ...
- Python Requests-学习笔记(9)-错误与异常
遇到网络问题(如:DNS查询失败.拒绝连接等)时,Requests会抛出一个ConnectionError 异常. 遇到罕见的无效HTTP响应时,Requests则会抛出一个 HTTPError 异常 ...
- [原创] 在C++中实现打字机效果
如题. void pout(string str,int t)//随便取的,不要介意,str是待输出字符串,t是每两个字的间隔时间. { ;i<str.length();i++) { cout& ...