Drainage Ditches jdoj-1817

    题目大意:网络流裸求最大流

    注释:n(点数),m(边数)<=200.

      想法:裸的网络流求最大流,用bfs+dfs,美其名曰dinic。

    没有开始?何来最后... ...代码... ...

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,ans;
int dis[300];
int head[210],to[500],nxt[500],f[500],cnt=1;
void add(int x,int y,int z)
{
to[++cnt]=y;
nxt[cnt]=head[x];
f[cnt]=z;
head[x]=cnt;
}
bool bfs()
{
queue<int> q;
memset(dis,-1,sizeof(dis));
while(!q.empty()) q.pop();
q.push(1),dis[1]=0;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=nxt[i])
if(f[i]&&dis[to[i]]<0)
{
dis[to[i]]=dis[x]+1;
q.push(to[i]);
if(to[i]==n) return true;
}
}
return false;
}
int dinic(int x,int flow)
{
int a,temp=flow;
if(x==n) return flow;
for(int i=head[x];i;i=nxt[i])
{
if(f[i]>0&&dis[to[i]]==dis[x]+1)
{
a=dinic(to[i],min(f[i],temp));
if(a==0) dis[to[i]]=-1;
temp-=a;f[i]-=a;f[i^1]+=a;
if(temp==0) break;
}
}
return flow-temp;
}
int main()
{
scanf("%d%d",&m,&n);
for(int x,y,z,i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);add(y,x,0);
}
while(bfs())
{
ans+=dinic(1,1<<30);
}
printf("%d",ans);
return 0;
}

    小结:bfs时需要加特判,加边时注意一些细节的错误。add时tot需要是1,不然我们通过tot^1寻找反向残流时会出现bug

[jdoj1817]Drainage Ditches_网络流的更多相关文章

  1. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  2. POJ 1273:Drainage Ditches 网络流模板题

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63339   Accepted: 2443 ...

  3. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

  4. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

  5. USACO 4.2 Drainage Ditches(网络流模板题)

    Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's fa ...

  6. NYOJ 323 Drainage Ditches 网络流 FF 练手

    Drainage Ditches 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Every time it rains on Farmer John's fields, ...

  7. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  8. Drainage Ditches~网络流模板

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

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

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

随机推荐

  1. mysql事务介绍

    什么是事务 一组sql语句操作单元组内所有sql完成一个业务如果整组成功,意味着组内的全部的sql成功如果其中任何一个失败,意味着整个操作失败 数据回到操作前的状态 事务的特点 1.多条sql语句组成 ...

  2. HI3531由DMA 发起PCIe 事务

    Hi3531 PCIe 控制器内含DMA 控制器,DMA 控制器包含有两个DMA 通道(一个 DMA 读通道和一个DMA 写通道).PCIe 控制器内包含的DMA 控制器用于大数据量 的存储器读写事务 ...

  3. 修改MyEclipse行数的颜色

    修改MyEclipse行数的颜色 1.未修改前,行数的颜色 2.依次选择"Window--->Preferences" 3.选择"General--->Edi ...

  4. Java Breakpoint

    1.错误描述 Java Breakpoint Unable to install breakpoint in com.you.humb.web.commom.dao.impl.ExportDaoImp ...

  5. struts2的配置文件

    struts2的配置文件 1.配置Action的struts.xml 2.配置Struts2有关属性的struts.properties

  6. TypeError:Error #1009

    1.错误描述 TypeError:Error #1009:无法访问空对象引用的属性和方法 2.错误原因 3.解决办法

  7. HTML5结合CSS的三种方法+结合JS的三种方法

    HTML5+CSS: HTML中应用CSS的三种方法 一.内联 内联样式通过style属性直接套进HTML中去. 示例代码 <pstylepstyle="color:red" ...

  8. Alice and Bob HDU - 4268

    Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...

  9. [BZOJ1007] [HNOI2008] 水平可见直线 (凸包)

    Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的. 例如,对于直线:L1:y=x ...

  10. textarea高度随内容自适应

    最近遇到一个需求,视频名称初始有个生成值,并且支持可以手动修改,修改后名称过长后换行高度随内容增加.刚开始想到用input但是发现input不支持换行.后来用了textarea,用js来控制,下面是实 ...