【算法】网络流-最大流(dinic)

【题解】http://www.cnblogs.com/onioncyc/p/6496532.html

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=,inf=0x3f3f3f3f;
struct edge{int from,v,flow;}e[maxn*];
int n,m,tot=,S,T,d[maxn],q[],first[maxn],cur[maxn];
void insert(int u,int v,int flow)
{tot++;e[tot].v=v;e[tot].flow=flow;e[tot].from=first[u];first[u]=tot;}
bool bfs()
{
memset(d,-,sizeof(d));
int head=,tail=;q[head]=S;
d[S]=;
while(head!=tail)
{
int x=q[head++];if(head>=)head=;
for(int i=first[x];i;i=e[i].from)
if(d[e[i].v]==-&&e[i].flow>)
{
q[tail++]=e[i].v;d[e[i].v]=d[x]+;
if(tail>=)tail=;
}
}
if(d[T]==-)return ;else return ;
}
int dfs(int x,int a)
{
if(x==T||a==)return a;
int flow=,f;
for(int& i=cur[x];i;i=e[i].from)//当前弧优化
if(d[e[i].v]==d[x]+&&(f=dfs(e[i].v,min(a,e[i].flow)))>)
{
e[i].flow-=f;
e[i^].flow+=f;
flow+=f;
a-=f;
if(a==)break;
}
return flow;
}
int main()
{
scanf("%d%d",&m,&n);
S=;T=n;
for(int i=;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
insert(u,v,w);
insert(v,u,);
}
int ans=;
while(bfs())
{
for(int i=;i<=n;i++)cur[i]=first[i];
ans+=dfs(S,inf);
}
printf("%d",ans);
return ;
}

【最大流】【CODEVS】1993 草地排水的更多相关文章

  1. Codevs 1993 草地排水

    1993 草地排水 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地 ...

  2. codevs 1993草地排水

    1993 草地排水

  3. 模板题 codevs 1993 草地排水 想学习的请看链接

    不能再水的题了. Dinic算法,比EK更快. 想要学习请看链接   https://comzyh.com/blog/archives/568/ 并附上我的模板(其实和comzyh大神的一样) #in ...

  4. codevs 1993 草地排水 USACO

    /*Dinic*/ #include<iostream> #include<cstdio> #include<cstring> #include<queue& ...

  5. CODEVS——T 1993 草地排水 USACO

    http://codevs.cn/problem/1993/  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 De ...

  6. 【CodeVS】1993草地排水

    题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水 ...

  7. 最大流 [USACO4.2]草地排水Drainage Ditches

    Background 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免 ...

  8. AC日记——草地排水 codevs 1993

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在农夫约翰的农场上,每 ...

  9. - > 网络流(【最大流】草地排水模板题)

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在农夫约翰的农场上,每 ...

随机推荐

  1. Lucene 高级搜索

    自定义评分 public class MyScoreQuery { public void searchByScoreQuery(){ try { IndexSearcher searcher=new ...

  2. 【第四周】psp

    代码累计 300+575+475+353=1603 随笔字数 1700+3000+3785+4210=12695 知识点 QT框架 Myeclipse基础环境 代码复用,封装 Ps技术 在excel画 ...

  3. Java实现的词频统计——单元测试

    前言:本次测试过程中发现了几个未知字符,这里将其转化为十六进制码对其加以区分. 1)保存统计结果的Result文件中显示如图: 2)将其复制到eclipse环境下的切分方法StringTokenize ...

  4. PAT L1-034 点赞

    https://pintia.cn/problem-sets/994805046380707840/problems/994805098188750848 微博上有个“点赞”功能,你可以为你喜欢的博文 ...

  5. TTPPRC —— 商业分析模型

    欢迎讨论 : ) 前言1 TTPPRC,是一个为了更容易.透切地进行商业分析而整理出的分析模型.通过这个模型,可以让不具备专业商业知识的大众都能容易得出商业分析结果. 此文是读者阅读原文后,而整理的一 ...

  6. git commad

    repo forall -c 'echo $REPO_PATH;ssh trd.git.htc.com -p29419 -lowen_wen gerrit create-project --submi ...

  7. C++函数中的那些坑

    平时写程序时,我们可能或多或少对一些用法感到朦胧,下面我对一些易困惑大家,或者易用错的地方作点介绍. 一.函数的一些注意点 1.函数返回类型不能是数组类型或函数类型,但可以是指向数组或函数的指针. 2 ...

  8. 题解 P1059 【明明的随机数】

    不会其他排序的小金羊又来水题了 本题我的思路:堆排,速度不需要算很快,AC就可以... 注意:初学者不宜抄此代码(压行严重) code: #include <cstdio> #includ ...

  9. 洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek

    题目戳 题目描述 Bessie is playing hide and seek (a game in which a number of players hide and a single play ...

  10. [COGS1000]伊吹萃香 最短路

    1000. [東方S2] 伊吹萃香 输入文件:suika.in   输出文件:suika.out   简单对比 时间限制:1 s   内存限制:128 MB Problem 4 伊吹萃香(suika. ...