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 ...
 
随机推荐
- html引入另一个html
			
在写页面的时候,有些东西是一样的,比如头部的导航或者尾部的标注.所以复用的东西可以写到一个文件中,之后再引入,angularjs或是jsp中都有很好的标签引入,而html没有,但是可以借助一些方式进行 ...
 - c++ 如何对拍
			
首先要写好两个要对拍程序(假设是A,B),和一个制造数据的程序(设为made) (要放在同一文件夹内) 编译得到A.exe , B.exe , made.exe 写一个对拍器 格式如下 @ech ...
 - 【学习笔记】深入理解js闭包
			
本文转载: 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接 ...
 - BottomNavigationBar底部导航条用法
			
先来张效果图 接下来是实现过程 1.加入依赖 compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0' 2.xml布局 fragment ...
 - K-means算法Java实现
			
public class KMeansCluster { private int k;//簇的个数 private int num = 100000;//迭代次数 ...
 - QT 学习笔记概述
			
以下笔记为在看书和实践的过程中的部分记录总结: 0. 窗口布局 1) 支持绝对布局和布局管理器布局; 2) 绝对布局不够灵活.无法自动调整大小,需要手动编写代码调整: 3) 布局管理器管理布局比较灵活 ...
 - windows下安装python
			
1. 进入python官网 https://www.python.org/downloads/windows/ 2.根据我们的电脑位数选择版本,尽量选择 Download Windows x86 ...
 - docker新手入门(基本命令以及介绍)
			
Docker 的核心内容 镜像 (Image) 容器 (Container) 仓库 (Repository) Registry 用来保存用户构建的镜像 docker的开始使用: 1. docker ...
 - 吸顶条 ---- jQ写法
			
<script> $(function () { var barTop = $('#bar').offset().top; //on方法相当于原生的绑定 $(window).on('scr ...
 - QT +坐标系统 + 自定义控件 + 对象树的验证(自动进行析构)_内存回收机制
			
通过创建一个新的按钮类,来进行析构函数的验证,即对象树概念的验证.当程序结束的时候会自动的调用析构函数, 验证思路: 要验证按钮会不会自动的析构,(即在QPushButton类里面的析构函数添加qDe ...