hdu 1532 Drainage Ditches(网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532
题目大意是:农夫约翰要把多个小池塘的水通过池塘间连接的水渠排出去,从池塘1到池塘M最多可以排多少的水流量,给定水渠和连接的两个小池塘,以及该水渠的流量,求最大流。
其实就是裸的最大流,源点是1号池塘,汇点是M号池塘,从1到M做一个dinic算法求出最大流就行,但是我一直WA,怎么也改不对,后来发现是因为自己喜欢用vector建边建图的原因,对于输入多组样例时,没有初始化上组样例的vector数组的数据,导致tle,wa各种错误,找了一个多小时才发现!下次记得要用clear()函数清除vector数组中的数据。
AC代码:
#include<iostream>
#include<stack>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int Ni = 205;
const int MAX = 0x3f3f3f3f;
struct node{
vector<int> vex;//某个节点连接的点
vector<int> num;//连接节点边的序号
}g[Ni];
struct edge{
int u,v,c;//u起点,v终点,c流量
}e[Ni*Ni];
int d[Ni];//深度
int N,M;
int edgenum = 0;//边的序号从0开始,
void addedge(int u,int v,int c){
e[edgenum].u = u;
e[edgenum].v = v;
e[edgenum].c = c;
g[u].vex.push_back(v);
g[u].num.push_back(edgenum++);
e[edgenum].u = v;
e[edgenum].v = u;
e[edgenum].c = 0;
g[v].num.push_back(edgenum++);
g[v].vex.push_back(u);
}
int bfs(){
memset(d,-1,sizeof(d));
queue<int> q;
q.push(1);
d[1] = 0;
while(!q.empty()){
int cur = q.front() ;
q.pop() ;
for(int i = 0;i<g[cur].vex.size() ;i++ ){
int now = g[cur].vex[i];
int te = g[cur].num[i];
if(d[now] == -1 && e[te].c > 0 ){//如果now没有访问过,且该边流量大于0
d[now] = d[cur] + 1;//增加深度
q.push(now);
}
}
}
return d[M]!=-1;
}
int dfs(int a,int b){
int r = 0;
if(a == M){
return b;
}
for(int i = 0;i<g[a].vex.size() && r<b ;i++ ){
int u = g[a].vex[i];
int te = g[a].num[i];
if(e[te].c > 0 && d[u] == d[a] + 1 ){
int t = min(e[te].c,b - r);//求出可以流过的流量
t = dfs(u,t);//递归寻找增广路
r+=t;
e[te].c-=t;
e[te^1].c+=t;
}
}
if(!r){
d[a] = -2;
}
return r;
}
int dinic(int sp,int tp){
int total = 0,t;
while(bfs()){
while(1){
t = dfs(sp,MAX);
if(!t){//找不到增广路,t=0,循环终止
break;
}
total+=t;
}
}
return total;
}
int main(){
while(~scanf("%d%d",&N,&M)){
for(int i = 0;i<N;i++){
int u,v,C;
scanf("%d%d%d",&u,&v,&C);
addedge(u,v,C);
}
printf("%d\n",dinic(1,M));
for(int i = 1;i<=M;i++){//!!!while输入多组样例,一定要清空vector数组内的数据!
g[i].num.clear() ;
g[i].vex.clear() ;
}
}
return 0;
}
hdu 1532 Drainage Ditches(网络流)的更多相关文章
- HDU 1532 Drainage Ditches(网络流模板题)
题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速, 本题就是让你求出最大流速,无疑要运用到求最大流了.题中m为水沟数, ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- 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(最大流 EK算法)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...
- 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) ...
随机推荐
- svn提交更新代码提示Please execute the 'Cleanup' command 的解决办法
那可能是提交或更新代码的过程意外终止,第二次提交或更新会报这个错误 更新或上传的时候动作没有完成,导致本地存在锁定状态没有释放 或者有文件正在更新或上传,该文件夹被锁定. 解决办法: 将对应文件夹里的 ...
- Linux的VMware虚拟机无法上网问题
很多时候,Linux无法上网,不管改成nat模式还是主机模式都不行.这时候可以选择进行重置: 首先点击编辑,之后点击虚拟网络编辑器, 然后移除VMnet0和VMnet8网络,点击确定: 然后再打开虚拟 ...
- 将Python模块转变为命令行工具
问:如何输入命令行就能执行python代码呢? 答:要将python模块转变为命令行工具只用在 setup.py 文件中添加参数entry_points 例如: entry_points={ 'con ...
- A - Wireless Network POJ - 2236-kuangbin带你飞
A - Wireless Network POJ - 2236 Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 50348 ...
- SpringBoot学习- 1、SpringSuit创建项目
SpringBoot学习足迹 前言:最近一次开发java后台应用还是三年前的2017年,主要使用SSH开发小型外包项目和公司的一个产品,感觉再不回顾下可能就要彻底忘记了,准备做一个后台管理项目练练手, ...
- leetcode腾讯精选练习之相交链表(六)
相交链表 题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5 ...
- js及jsp.java查错的几种方式
一.js 1.console.log("你想输出的内容"); 2.alert("你想输出的内容"); 3.debugger;(记得打开F12) 4.快速找到js ...
- 【Unity|C#】基础篇(7)——属性(Property)/ 索引器(Indexer)
[学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...
- 查询linux版本信息
uname -a (Linux查看版本当前操作系统内核信息) cat /proc/version (Linux查看当前操作系统版本信息) cat /etc/redhat-release (Linux查 ...
- Keep-Alive 以及服务器心跳
Keep-Alive 来源 :http://www.nowamagic.net/academy/detail/23350305 服务器心跳 来源 :http://www.cnblogs.com/lw ...