HDU-3549 最大流模板题
1、HDU-3549 Flow Problem
2、链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549
3、总结:模板题,参考了 http://www.cnblogs.com/Lyush/archive/2011/08/08/2130660.html ,里面有几种模板,没太看懂
题意:给定有向图,求第1到第n个点的最大流。
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const int N=; int mapn[][],flow[][]; //mapn存储边最大容量,flow存储边实际容量
int path[],pre[]; //path存储当前结点流量,pre存储父亲路径
int n,m; int bfs()
{
memset(path,,sizeof(path));
memset(pre,,sizeof(pre)); queue<int>q;
q.push();
int pos;
path[]=INF; //下面要比较到path[1]
while(!q.empty())
{
pos=q.front();
q.pop();
for(int i=;i<=n;i++)
{
if(!path[i]&&flow[pos][i]<mapn[pos][i]){
//如果这条边没有饱和或者形成了回流,就更新当前结点的流量 path[i]=min(path[pos],mapn[pos][i]-flow[pos][i]);
//即path[i]要取这条增广路径上残余流量的最小值 pre[i]=pos; //记录下父亲路径
q.push(i);
}
}
} return path[n];
} int Max_Flow()
{
int ans=,pos,path_n;
memset(flow,,sizeof(flow)); while(true)
{
path_n=bfs(); //每一次bfs获得一条增广路径
if(!path_n)return ans; //如果找不到增广路径就跳出
pos=n;
while(pos!=){ //更新flow
flow[pre[pos]][pos]+=path_n;
flow[pos][pre[pos]]-=path_n;
pos=pre[pos];
}
ans+=path_n;
} } int main()
{
int t;
scanf("%d",&t);
for(int test=;test<=t;test++)
{
memset(mapn,,sizeof(mapn));
scanf("%d%d",&n,&m);
int x,y,c;
while(m--){
scanf("%d%d%d",&x,&y,&c);
mapn[x][y]+=c;
} int max_flow;
max_flow=Max_Flow();
printf("Case %d: %d\n",test,max_flow); } return ;
}
HDU-3549 最大流模板题的更多相关文章
- 【网络流#1】hdu 3549 - 最大流模板题
因为坑了无数次队友 要开始学习网络流了,先从基础的开始,嗯~ 这道题是最大流的模板题,用来测试模板好啦~ Edmonds_Karp模板 with 前向星 时间复杂度o(V*E^2) #include& ...
- HDU 1532 最大流模板题
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU-3549Flow Problem 最大流模板题
传送门 这里是Ford-Fulkerson写的最大流模板 #include <iostream> #include <cstdio> #include <algorith ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
随机推荐
- 图结构练习——最短路径(dijkstra算法(迪杰斯拉特))
图结构练习——最短路径 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个带权无向图,求节点1到节点n的最短路径. ...
- golang channel basic
package mainimport ( "fmt" "math/rand" "time")func main() { rand.Seed( ...
- .Net Ioc Unity
Unity 的接口IUnityContainer public interface IUnityContainer : IDisposable IUnityContainer RegisterType ...
- WPF ListView展示层叠信息
通常我们在ListView中展示一列同类数据,例如城市名称.不过可以对ListView的DataTemplate稍作修改,让其显示层叠信息.例如:需要在ListView中显示省份和省份对应的城市名称. ...
- Ubuntu下Chromium for Android 源码的编译
转自:http://blog.csdn.net/leer168/article/details/9146689 一.环境Ubuntu10.4.4 -desktop-amd64 + VMware Wor ...
- markdown使用总结
# markdown简介> Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档,然后转换成格式丰富的HTML页面. —— [维基百科]( https://zh.w ...
- 关于移动端1px边框问题
<div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...
- 【Web前端】---js调用本地应用程序
最近进入了一个项目组,向大牛们一起学习如何搞开发,可谓是边开发边学习.就在前两天,我们的项目被领导们验收了一次,顺便给我们提了点新的需求,要求我们能够使用外在设备拍照上传.君要臣死,臣不能不死.更何况 ...
- 及其简短的Splay代码
#include <stdio.h> #include <queue> #include <algorithm> #include <stdlib.h> ...
- Android自动化测试之Monkey Test(一)
Monkey是什么 Monkey是可以运行在模拟器里或实际设备中的程序.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试. Monkey简 ...