http://poj.org/problem?id=1273

Dinic算法

这是一道最大流的经典题

最大流尽量应该用边表,优于邻接矩阵(所以我写了邻接矩阵版的之后又写了个边表)

用了新学的Dinic算法

在每次找增广路之前先跑一遍bfs,把所有点的深度标记,按bfs记下的顺序找增广路,这条增广路上最小的边容量就是这条增广路的最大费用

跑出一条增广路之后,就开始更新现在的边剩余的容量,并建立反向边。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,map[][],ans,pre[];
void Flow(int s,int t){
while(){
queue<int>q;
q.push();
memset(pre,,sizeof(pre));
while(!q.empty()){
int point=q.front();
q.pop();if(point==t)break;
for(int i=;i<=n;i++){
if(map[point][i]&&!pre[i])pre[i]=point,q.push(i);
}
}if(pre[t]==)break;
int so=0x7fffffff;
for(int i=t;i!=s;i=pre[i]){
so=min(so,map[pre[i]][i]);
}ans+=so;
for(int i=t;i!=s;i=pre[i]){
map[i][pre[i]]+=so;
map[pre[i]][i]-=so;
}
}
}
int main(){
while(scanf("%d%d",&m,&n)!=EOF){
int f,t,v;
memset(map,,sizeof(map));
for(int i=;i<=m;i++){
scanf("%d%d%d",&f,&t,&v);
map[f][t]+=v;
}
ans=;
Flow(,n);
printf("%d\n",ans);
}
}

poj1273 Drainage Ditches (最大流模板)的更多相关文章

  1. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  2. poj-1273 Drainage Ditches(最大流基础题)

    题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted ...

  3. POJ-1273 Drainage Ditches 最大流Dinic

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...

  4. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  5. poj1273 Drainage Ditches (最大流板子

    网络流一直没学,来学一波网络流. https://vjudge.net/problem/POJ-1273 题意:给定点数,边数,源点,汇点,每条边容量,求最大流. 解法:EK或dinic. EK:每次 ...

  6. [poj1273]Drainage Ditches(最大流)

    解题关键:最大流裸题 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  7. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  8. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  9. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  10. 2018.07.06 POJ1273 Drainage Ditches(最大流)

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...

随机推荐

  1. JSP九大内置对象的作用和用法总结(转发)

    jsp九大内置对象: 内置对象/作用域(每一种作用域的生命周期是不一样的): 1, application 全局作用域 2, session 会话作用域 3, request 请求作用域 4, pag ...

  2. LeetCode:分发饼干【455】

    LeetCode:分发饼干[455] 题目描述 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的 ...

  3. systemclock sleep 睡眠

  4. IDEAL葵花宝典:java代码开发规范插件 p3c

    前言: P3C插件 是阿里巴巴p3c项目组进行研发.这个项目组是阿里巴巴开发爱好者自发组织形成的虚拟项目组,根据<阿里巴巴Java开发规范>转化而成的自动化插件,并且实现了部分自动编程. ...

  5. checkbox怎么判断是否选中

    下面这种可以使用 if($("#checkbox1").is(':checked')) { alert("1"); } else { alert("0 ...

  6. css书写规则

    无规矩不成方圆,不管有多少人共同参与同一项目,一定要确保每一行代码都像是同一个人编写的 不要在自闭合(self-closing)元素的尾部添加斜线 不要省略可选的结束标签(closing tag)(例 ...

  7. ICE 迁移64位安装问题

    昨天手贱,在apt-get install 后有一大堆,上百个安装包not upgrade, 发现有提示apt-get autoremove,犹豫了很久后还是忍不住执行了autoremove:这个命令 ...

  8. BZOJ1251 序列终结者(Splay平衡树)(占位)

    网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量…… ...

  9. 「SHOI2002」「LuoguP1291」百事世界杯之旅(UVA10288 Coupons)(期望,输出

    题目描述 “……在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽奖活动,获得球星背包,随声听,更克赴日韩观看世界杯.还不赶 ...

  10. ACM学习历程—HDU4717 The Moving Points(模拟退火 || 三分法)

    Description There are N points in total. Every point moves in certain direction and certain speed. W ...