[POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

题意
网络流最大流模板
思路
EK也不会超时
所以说是一个数据比较水的模板题
但是POJ有点坑,多组数据,而且题目没给
哭得我AC率直掉

代码
用的朴素Dinic
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define N 200010
#define INF 0x3fffffff
inline int read() {
int x = ,f = ;
char ch = getchar();
while(ch<''||ch>'') { if(ch=='-') f = -; ch = getchar(); }
while(ch>=''&&ch<='') {x=(x<<)+(x<<)+(ch^);ch=getchar();}
return x * f;
}
struct point{
int fr,to,val;
int nxt;
}edge[N];
int head[N];
int cnt;
void add_edge(int x,int y,int z)
{
edge[cnt].fr=x,edge[cnt].to=y,edge[cnt].val=z;
edge[cnt].nxt=head[x],head[x]=cnt++;
edge[cnt].fr=y,edge[cnt].to=x,edge[cnt].val=;
edge[cnt].nxt=head[y],head[y]=cnt++;
}
//我放弃链式前向星
int n,m,st,ed;
int deep[N];
int BFS()
{
queue<int>q;
memset(deep,,sizeof(deep));
q.push(st);
deep[st]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt)
{
int tmp=edge[i].to;
if(edge[i].val<= || deep[tmp]) continue;
deep[tmp]=deep[u]+;
q.push(tmp);
}
}
return deep[ed];
}
int ans;
int dfs(int u,int flow)//flow为到达终点最多能增广的值
{
if(u==ed) return flow;
int add=;
for(int i=head[u];i!=-&&add<flow;i=edge[i].nxt)
{
int v=edge[i].to;
if(deep[v]!=deep[u]+) continue;
if(!edge[i].val) continue;//剪枝
int tmpadd=dfs(v,min(edge[i].val,flow-add));
edge[i].val-=tmpadd;
edge[i^].val+=tmpadd;//sub
add+=tmpadd;
}
return add;
}
void Dinic()
{
while(BFS()) ans+=dfs(st,INF);
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
ans=;
st=,ed=m;
memset(edge,,sizeof(edge));
memset(head,-,sizeof(head));
for(int i=,u,v,co;i<=n;i++)
{
u=read(),v=read(),co=read();
add_edge(u,v,co);
}
Dinic();
printf("%d\n",ans);
}
return ;
}
[POJ1273][USACO4.2]Drainage Ditches (网络流最大流)的更多相关文章
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- 【USACO4.2】草地排水Drainage Ditches(最大流)
题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...
- HDU1532 Drainage Ditches 网络流EK算法
Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...
- poj 1273 (nyoj 323) Drainage Ditches : 最大流
点击打开链接 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49648 Accepte ...
随机推荐
- Exp1:PC平台逆向破解 20164314 郭浏聿
一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getS ...
- Linux基础 - 基本命令
一.Linux系统命令操作语法格式 二.目录操作 创建目录 查看目录 改变当前的目录/位置 打印当前工作目录 三.创建文件或者修改文件时间戳(文件属性) 四.stat命令 vim 查看文件内容 mor ...
- iTOP-iMX6UL开发板【全能版】-动态调频技术简介
本文档以 iMX6UL 为例,简单介绍 cpufreq 的 5 种模式. 在 imx6ul 的 menuconfig 中,进入 CPU Power Management ---> CPU Fre ...
- js关联数组
标准javascript 是内含支持hash关联数组,经查找资料并测试,有关标准javascript内含的hash关联数组操作备忘如下 1.Hash关联数组定义 // 定义空数组 myhash = { ...
- 【软工作业&思考】关于软工的一些概念性理解暨第一次阅读作业
概述 项目 内容 本次作业所属课程 2019BUAA软件工程 周二班 本次作业要求 第1次个人作业当然,比这个更重要百倍的还是实实在在的思考,这也是标题如此命名的原因 我在本课程的目标 在原有实践经验 ...
- 一、Python学习之路
基础篇 第一章 Python介绍.安装.使用 Python 简介 Python 安装 第一个Python程序 Python 解释器 字符编码与解码 动态语言与静态语言的区别 变量及简单 ...
- C#创建 WebApi 项目
做web api 可以参考一下网友 C#进阶系列——WebApi 接口参数不再困惑:传参详解 - 懒得安分 - 博客园https://www.cnblogs.com/landeanfen/p/5337 ...
- C# - 学习总目录
C# - 基础 C# - 操作符 C# - 值类型和引用类型 C# - 表达式与语句 C# - 数组 C# - 引用类型 C# - 常用类 C# - 常用接口 C# - LINQ 语言集成查询 C# ...
- [转] C/C++ 调用Python
from : https://cyendra.github.io/2018/07/10/pythoncpp/ 目录 前言 官方文档 环境搭建 编译链接 Demo 解释器 初始化 GIL Object ...
- 记一次简单的PHP代码审计(SSRF案例)
题目链接: http://oj.momomoxiaoxi.com:9090/ 用dirsearch对网址进行扫描,发现robots.txt 命令行: python3 dirsearch.py -u & ...