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) ...
随机推荐
- mysql 获取当前时间加上一个月
select DATE_ADD(NOW(), interval 1 MONTH) NOW()此处必须为时间格式 date_add() 增加 date_sub()减少 month 月份 minute 分 ...
- python3练习100题——051
题目:学习使用按位与 & . 不会的知识点,查了一下按位运算. 按位运算符是把数字看作二进制来进行计算的. 运算符 描述 实例 & 按位与运算符:参与运算的两个值,如果两个相应位都为1 ...
- GYCTF Node game
考点: NodeJS 代码审计 SSRF 请求夹带 复现: 不太懂js,先留着吧,学懂了再记录
- jQuery---音乐导航
音乐导航 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- C 库函数 - fmod()
C 库函数 - fmod() 转自: C 标准库 - <math.h> 描述 C 库函数 double fmod(double x, double y) 返回 x 除以 y 的余数. 声明 ...
- FatMouse and Cheese HDU - 1078 dp
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int n,k ...
- Postgresql Json Sql
a detailed website about json sql query; official website: here, chinese version: here Json query: - ...
- linux - python:卸载
[root@test ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制删除已安装程序及其关联[root@test ~]# ...
- JUC-JUC是什么?
一.JUC是什么? java.util.concurrent在并发编程中使用的工具类 进程/线程回顾 1.进程/线程是什么? 进程:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动.它是 ...
- ubuntu apt
一些命令: sudo apt-get update 更新源 sudo apt-get install package --reinstall 重新安装包 sudo apt-get upgrade ...