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

题目大意:

n点m边网络流,求1-n最大流。

——————————————

网络流板子,切了。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=;
const int INF=;
inline int read(){
int X=,w=;char ch=;
while(ch<''||ch>''){w|=ch=='-';ch=getchar();}
while(ch>=''&&ch<='')X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int next;
int to;
int w;
}edge[*maxn];
int head[*maxn],cnt=-;
void add(int u,int v,int w){//u起点v终点w容量
cnt++;
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
return;
}
int lev[maxn],cur[maxn];//lev层数,cur[i]为以i为起点的边的编号
bool bfs(int m){//强制1为源点,m为汇点
int dui[m],r=;//队列和右指针
for(int i=;i<=m;i++){//初始化
lev[i]=-;
cur[i]=head[i];
}
dui[]=,lev[]=;
int u,v;//u起点v终点
for(int l=;l<=r;l++){//左指针
u=dui[l];
for(int e=head[u];e!=-;e=edge[e].next){
v=edge[e].to;
if(edge[e].w>&&lev[v]==-){//1.能走 2.未分层
lev[v]=lev[u]+;
r++;
dui[r]=v;//v入队
if(v==m)return ;//分层完毕
}
}
}
return ;//无法分层
}
int dinic(int u,int flow,int m){//u当前点,flow为下面的点能够分配多大的流量,m终点
if(u==m)return flow;//终点直接全流入
int res=,delta;//res实际流量
for(int &e=cur[u];e!=-;e=edge[e].next){//'&'相当于cur[u]=e;即流满的边不会再被扫一次
int v=edge[e].to;
if(edge[e].w>&&lev[u]<lev[v]){//只能从低层往高层流
delta=dinic(v,min(edge[e].w,flow-res),m);
if(delta>){//如果增广
edge[e].w-=delta;//正向边容量减少
edge[e^].w+=delta;//反向边仍量增加(暗示退流)
res+=delta;//扩张流量增加
if(res==flow)break;//可流的都流完了,及时跳出
}
}
}
if(res!=flow)lev[u]=-;//没流完,说明以后不能从这个点流出任何流量,那就不需要这个点了
return res;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
cnt=-;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
int s=read();
int e=read();
int c=read();
add(s,e,c);
add(e,s,);
}
int ans=;
while(bfs(m)==)ans+=dinic(,INF,m);
printf("%d\n",ans);
}
return ;
}

POJ1273:Drainage Ditches——题解的更多相关文章

  1. poj1273 Drainage Ditches Dinic最大流

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

  2. 【网络流】POJ1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78671   Accepted: 3068 ...

  3. POJ 1273 Drainage Ditches题解——S.B.S.

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67823   Accepted: 2620 ...

  4. poj1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68414   Accepted: 2648 ...

  5. POJ-1273 Drainage Ditches 最大流Dinic

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

  6. 2018.07.06 POJ1273 Drainage Ditches(最大流)

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

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

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

  8. POJ1273 Drainage Ditches (网络流)

                                                             Drainage Ditches Time Limit: 1000MS   Memor ...

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

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

随机推荐

  1. vim分屏功能总结

    vim的分屏功能 总结起来,基本都是ctrl+w然后加上某一个按键字母,触发一个功能.(1)在shell里打开几个文件并且分屏: vim -On file1 file2 ... vim -on fil ...

  2. Java连接redis集群操作存储、删除以及获取值

    pom文件添加: <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> &l ...

  3. 文件包含漏洞(RFI)

    1文件包含漏洞简介 include  require  include_once   require_once RFI综述 RFI是Remote File Inclusion的英文缩写,直译过来就是远 ...

  4. 前端开发工程师 - 01.页面制作 - 第4章.CSS

    第4章.CSS CSS简介 Cascading Style Sheet 层叠样式表:定义页面中的表现样式 history: CSS1(1996)--CSS2(1998)--着手CSS3草案(拆分成很多 ...

  5. Android softkeyboard 和 其他界面关系 softInputMode

    转 : http://blog.csdn.net/xww810319/article/details/17397429 and http://blog.csdn.net/harryweasley/ar ...

  6. 使用flask_limiter设定API配额

    前言 闲来无事,突然想到了以前做过的关于后台API安全方面的事,关于接口访问配额的设置,flask有没有很好的库支持呢?一找还真有!主要是对照了库的官方文档自己写了下dome,以供参考. # -*- ...

  7. mpvue笔记

    简介: mpvue 修改了 Vue.js 的 runtime 和 compiler 实现,为小程序开发引入 Vue.js 开发体验 我觉得就像scss一样,写的时候方便,最后还是要转成css文件 搭建 ...

  8. 142. O(1) Check Power of 2【LintCode by java】

    Description Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return t ...

  9. 树(Tree,UVA 548)

    题目描述: 题目思路: 1.使用数组建树 //递归 2.理解后序遍历和中序遍历,建立左右子树 3.dfs深度搜索找出权重最小的路径 #include <iostream> #include ...

  10. 【转】cocos2d-x如何优化内存的应用

    原地址:http://cblog.chinadaily.com.cn/blog-942327-4327173.html 注:自身以前也写过cocos2d-x如何优化内存的应用,以及内存不够的情况下怎么 ...