HDU 1532 Drainage Ditches 排水渠(最大流,入门)
题意:
给出一个有向图,以及边上的容量上限,求最大流。(有重边,要将容量上限叠加)
思路:
用最简单的EK+BFS解决。每次搜到一条到达终点的路径,就立刻退出,更新ans,然后再回头修改图中的当前flow状况(这就得靠记录路径了)。当当前图没有到达终点的路径图,流已经饱和,可以结束程序了。
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+; vector<int> vect[N];
int c[N][N]; //容量
int flow[N][N]; //流量 int a[N]; //临时流量
int path[N]; //得记录用的是哪条边,好更新flow和cap int BFS(int m)
{
deque<int> que;
que.push_back();
a[]=INF; //先置为无穷大 while(!que.empty())
{
int x=que.front();
que.pop_front();
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(!a[t] && c[x][t]>flow[x][t] ) //未遍历过,且容>流
{
path[t]=x; //只需要记得到达t的是哪个点
a[t]=min(a[x], c[x][t]-flow[x][t]); //要么全部流给你,要么取能流过的上限
que.push_back(t);
}
}
if(a[m]) return a[m]; //只要有路径能够更新到终点m,立刻退出。
}
return ;
} int cal(int m)
{
int ans_flow=;
while(true) //求最大流
{
memset(a,,sizeof(a));
memset(path,,sizeof(path)); int tmp=BFS(m);
if(!tmp) return ans_flow; //找不到增广路了
ans_flow+=tmp; int ed=m;
while(ed!=) //根据路径调整一下流及上限
{
int from=path[ed];
flow[from][ed]+=tmp; //正向边加流量
flow[ed][from]-=tmp; //反向边减流量,相当于cap-flow一样大于0。
ed=from;
}
}
} int main()
{
freopen("input.txt", "r", stdin);
int n, m, st, ed, ca;
while(~scanf("%d%d",&n,&m))
{ for(int i=; i<=m; i++) vect[i].clear();
memset(c, , sizeof(c));
memset(flow, , sizeof(flow)); for(int i=; i<n; i++)
{
scanf("%d %d %d", &st, &ed, &ca);
vect[st].push_back(ed); //邻接表
vect[ed].push_back(st); //反向边,容量是0的。
c[st][ed]+=ca; //坑在这
}
cout<<cal(m)<<endl; }
return ;
}
AC代码
HDU 1532 Drainage Ditches 排水渠(最大流,入门)的更多相关文章
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- USACO93网络流入门Drainage Ditches 排水渠(DCOJ 5130)
题目描述 (传送门:http://poj.org/problem?id=1273翻译 by sxy(AFO的蒟蒻)) 每次约翰的农场下雨,Bessie的水池里的四叶草就会被弄破.这就意味着,这些四叶草 ...
- hdu 1532 Drainage Ditches (最大流)
最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...
随机推荐
- Oracle用户进程跟踪
用户进程跟踪 分为 基于会话级别跟踪和 实例级别跟踪: 会话级别跟踪又包括 当前会话跟踪和 非当前会话跟踪 跟踪文件位置由user_dump_dest设定,大小由max_dump_file_size ...
- 【转】 GDB 常用调试方法
一.多线程调试 多线程调试可能是问得最多的.其实,重要就是下面几个命令: info thread 查看当前进程的线程. thread <ID> 切换调试的线程为指定ID的线程. break ...
- Oracle外部表的使用
外部表可以像其它表一样,用select语句作查询.但不能做DML操作,不能建index,不接受约束.这是因为它不是以段的形式存于数据库中,只是以数据字典构造存在,指向一个或多个操作系统文件. 外部表的 ...
- WPF系列
一.ListView绑定数据源XML //前端代码 1 <Window x:Class="ListView读取XML数据.MainWindow" xmlns="ht ...
- linux 常用命令 集锦
第一章 LINUX简介及安装 1一.LINUX介绍 1二.LINUX安装 2三.LINUX目录 2四.总结来说: 3第二章 常用命令及帐户管理 4一.linux命 ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- MySQL中字符串函数详细介绍
MySQL字符串函数对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str)返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回N ...
- [搜片神器]BT种子下载超时很多的问题分析
继续接着第一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: h31bt.org 大家可以给提点意 ...
- 【转载】test和cmp比较
标 题:test和cmp一个很菜很基础的话题! 作 者: FTBirthday 时 间:2003/05/19 01:14am 链 接:http://bbs.pediy.com 看过破解教程,都知道te ...
- python:Attempted relative import in non-package
problem:Attempted relative import in non-package 所谓相对路径其实就是相对于当前module的路径,但如果直接执行脚本,这个module的name就是“ ...