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 ...
随机推荐
- php url地址栏传中文乱码解决方法集合
php地址栏传中文$_GET下来后乱码,urlencode和urldecode,iconv,base64_encode等方法,整理基本是常用的了. php地址栏传中文$_GET下来后乱码,urlen ...
- 动手实现 React-redux(二):结合 context 和 store
既然要把 store 和 context 结合起来,我们就先构建 store.在 src/index.js 加入之前创建的 createStore 函数,并且构建一个 themeReducer 来生成 ...
- Oracle 十大SQL语句
oracle数据库十大SQL语句 操作对象(object) /*创建对象 table,view,procedure,trigger*/ create object object ...
- 【学习笔记】C++文件操作详解(ifstream、ofstream、fstream)
C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifstream: 读操作(输入)的文件类(由istream引申而来) fstre ...
- 【经验总结】OSG 安装配置
对于普通用户推荐直接下载安装包配置.如有特殊需求或想了解编译过程可参考网上文章自己编译后配置.(通常建议使用第一种方法即可) 本人安装经验: 失败:自己系统64位,VS2010 32位,开始自己动手编 ...
- iOS 从相册中拿到 图片名 ,截取后缀,图片名
//从路径中获得完整的文件名 (带后缀) 对从相册中取出的图片,视频都有效. NSString *fileName = [filePath lastPathComponent]; //获得文件名 (不 ...
- Xilinx器件原语
原语,其英文名为primitive,是FPGA厂商针对其器件特征开发的一系列常用模块的名称.原语是FPGA芯片中基本元件,代表FPGA中实际拥有的硬件逻辑单元,如LUT,D触发器,RAM等.相当于软件 ...
- SPI总线小结
串行外设接口(Serial Peripheral Interface,SPI)的缩写.是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线.Motorola首先在其MC68HCXX系列 ...
- webapi之fiddler头设置
Host: localhost:16648Connection: keep-aliveContent-Length: 36Accept: application/json, text/javascri ...
- Qt和Cocoa混合编程
https://el-tramo.be/blog/mixing-cocoa-and-qt/