POJ 1273 Drainage Ditches【图论,网络流】
就是普通的网络流问题,想试试新学的dinic算法,这个算法暑假就开始看国家集训队论文了,之前一直都只用没效率的EK算法,真正学会这个算法还是开学后白书上的描述:dinic算法就是不断用BFS构建层次图然后用DFS寻找增广。然后就是一下午的WA ,除了第一次调dinic的问题外,这道题竟然有多组数据!!!看discuss里好像还有重边的问题,可我用的邻接表有效避免了这个问题~~
#include <iostream>
#include <cstdio>
#include<string.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=600;
inth=0,dist[maxn]={0},nex[maxn*4+20]={0},root[maxn]={0},point[maxn*4+20]={0},flow[maxn*4+20]={0};
int visit[maxn]={0},m;
int min(int a,int b)
{
int ret=a<b ? a : b;
return ret;
}
void add(int x,int y,int c)
{
nex[++h]=root[x];
point[h]=y;
flow[h]=c;
root[x]=h;
}
void bfs(int src)
{
memset(visit,0,sizeof(visit));
memset(dist,0,sizeof(dist));
int l=0,r=1,que[50000]={0};
que[++l]=src;
visit[src]=1;
while(l<=r)
{
int u=que[l++];
for(int i=root[u];i!=0;i=nex[i])
{
if (flow[i]!=0 && visit[point[i]]==0)
{
que[++r]=point[i];
dist[point[i]]=dist[u]+1;
visit[point[i]]=1;
}
}
}
}
int dfs(int u,int d)
{
if(u==m)return d;
int ret=0;
for(int i=root[u];i!=0 && d;i=nex[i])
{
if (flow[i]!=0 && dist[point[i]]==dist[u]+1)
{
int dd=dfs(point[i],min(d,flow[i]));
flow[i]-=dd;
flow[((i-1) ^ 1)+1]+=dd;//这个构造最赞~~!!
d-=dd;
ret+=dd;
}
}
return ret;
}
int main()
{
int x,y,c,ret;
int n;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(root,0,sizeof(root));
memset(nex,0,sizeof(nex));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&x,&y,&c);
add(x,y,c);
add(y,x,0);
}
ret=0;
while (1)//dinic
{
bfs(1);
if (visit[m]==0)break;
ret+=dfs(1,inf);
}
printf("%d\n",ret);
}
return 0;
}
POJ 1273 Drainage Ditches【图论,网络流】的更多相关文章
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches
Drainage Ditches 题目抽象:给你m条边u,v,c. n个定点,源点1,汇点n.求最大流. 最好的入门题,各种算法都可以拿来练习 (1): 一般增广路算法 ford() #in ...
- POJ 1273 Drainage Ditches 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- 网络流--最大流--POJ 1273 Drainage Ditches
链接 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clov ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
随机推荐
- 在 Windows 7 中禁用IPv6协议/IPv6隧道
How to disable certain Internet Protocol version 6 (IPv6) components in Windows Vista, Windows 7 and ...
- 代码审查的艺术:Dropbox 的故事
Dropbox 的 iOS 应用中的每一行代码,都是开始于被添加到 Maniphest 中的一个 bug 或者功能任务,Maniphest 是我们的任务管理系统.当一位工程师在上面接受一个任务,那么在 ...
- 备忘录模式及php实现
备忘录模式: 又叫做快照模式或Token模式,在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 角色: 1.创建者:负责创建一个备忘 ...
- 亲身经历,Java面试题整理
博主在2015年暑期参加过一些Java开发工程师实习的面试和笔试,在此将重点整理出来,以供大家学习. 资料1: 一.单继承 1.1Java类是否支持多重继承? 答:继承的基本原则是: 子类继承父类的所 ...
- 新建cordova应用
使用命令行(本例命令行均使用as或webstrom的命令行),在任意目录输入以下命令新建cordova应用 cordova create capp1 com.cesc.ewater.capp1 其中c ...
- 《Python基础教程》 读书笔记 第六章 抽象 函数 参数
6.1创建函数 函数是可以调用(可能包含参数,也就是放在圆括号中的值),它执行某种行为并且返回一个值.一般来说,内建的callable函数可以用来判断函数是否可调用: >>> x=1 ...
- ubuntu上部署windows开发的dotnet core程序
目标:完成windows上开发的dotnet core程序部署至linux服务器上(Ubuntu 14.04) windows上开发dotnet core很简单,安装好VS2017,建立相关类型的项目 ...
- Sql Server数据库对象访问权限控制
以下内容主要针对database层面的数据访问权限(比如select, insert, update, delete, execute…) 1.直接给user权限GRANT EXECUTE TO [u ...
- vue同胞组件通讯解决方案(以下为一种另外可用vuex解决)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- SQLite – GROUP BY
SQLite - GROUP BY SQLite GROUP BY子句中使用与SELECT语句的合作安排相同的数据组. 在GROUP BY子句之前一个SELECT语句的WHERE子句,先于ORDER ...