poj 1273 Drainage Ditches_最大流模版

时间:2021-12-02 13:15:14
#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define INF 0xfffffff
#define N 210
int cap[N][N],flow[N][N];
int pre[N],dist[N];
int ek(int sta,int end){
int i,curr,sum=0;
memset(flow,0,sizeof(flow));
while(1){
memset(pre,0,sizeof(pre));
memset(dist,0,sizeof(dist));
queue<int> q;
q.push(sta);
dist[sta]=INF;
pre[sta]=sta;
while(!q.empty()){
curr=q.front();
q.pop();
for(i=1;i<=end;i++)
if(!dist[i]&&flow[curr][i]<cap[curr][i]){
dist[i]=min(dist[curr],cap[curr][i]-flow[curr][i]);
pre[i]=curr;
q.push(i);
}
}
if(dist[end]==0)
break;
for(i=end;i!=sta;i=pre[i]){
curr=pre[i];
flow[curr][i]+=dist[end];
flow[i][curr]-=dist[end];
}
sum+=dist[end];
}
return sum;
} int main(int argc, char** argv) {
int n,m,i,a,b,w;
while(scanf("%d%d",&n,&m)!=EOF){
memset(cap,0,sizeof(cap));
for(i=1;i<=n;i++){
scanf("%d%d%d",&a,&b,&w);
cap[a][b]+=w;
}
printf("%d\n",ek(1,m));
}
return 0;
} #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 210
#define INF 0xfffffff
int flow[N][N],dist[N],m,n;
int bfs(){
memset(dist,-1,sizeof(dist));
dist[1]=0;
queue<int> q;
q.push(1);
while(!q.empty()){
int k=q.front();
q.pop();
for(int i=1;i<=n;i++){
if(flow[k][i]>0&&dist[i]<0){
dist[i]=dist[k]+1;
q.push(i);
}
}
}
if(dist[n]>0) return 1;
else return 0;
}
int dfs(int x,int mx){
int i,a;
if(x==n) return mx;
for(i=1;i<=n;i++){
if(flow[x][i]>0&&dist[i]==dist[x]+1&&(a=dfs(i,min(mx,flow[x][i])))){
flow[x][i]-=a;
flow[i][x]+=a;
return a;
} }
return 0;
}
int main(){
int i,b,a,w,ans,res;
while(scanf("%d%d",&m,&n)!=EOF){
memset(flow,0,sizeof(flow));
for(i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&w);
flow[a][b]+=w;
}
ans=0;
while(bfs())
while(res=dfs(1,INF))
ans+=res;
printf("%d\n",ans);
}
}

poj 1273 Drainage Ditches_最大流模版的更多相关文章

  1. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  2. POJ 1273 - Drainage Ditches - &lbrack;最大流模板题&rsqb; - &lbrack;EK算法模板&rsqb;&lbrack;Dinic算法模板 - 邻接表型&rsqb;

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

  3. Poj 1273 Drainage Ditches&lpar;最大流 Edmonds-Karp &rpar;

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

  4. POJ 1273 Drainage Ditches 最大流

    这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...

  5. POJ 1273 Drainage Ditches &vert; 最大流模板

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

  6. POJ 1273 Drainage Ditches&lpar;最大流Dinic 模板&rpar;

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  7. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  8. POJ 1273 Drainage Ditches &lpar;网络最大流&rpar;

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  9. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

随机推荐

  1. 深入研究java&period;lang&period;Runtime类【转】

    转自:http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 Runtime getRuntime e ...

  2. c&num;中winform的MVP模式的简单实现

    MVP模式是类似于MVC模式的一种设计模式,最近在做项目学习过程中遇到,弄了很久终于有一些眉目,这是学习过程中的一些笔记.MVP指的是实体对象Model.视图Viw和业务处理Presenter.MVP ...

  3. linux浏览器&comma;邮件客户端&comma;输入法&comma;双屏设置&comma;应用软件&comma;scrot -s截图&comma;office

    搜狗输入法linux版:http://pinyin.sogou.com/linux/help.php win/linux同时支持比较好用的浏览器:maxthon,firefox,maxthon,ope ...

  4. NUnit&plus;mock&plus;moq单元测试

    [TestFixture] public class InstantBatchBuyTest { private string _mallAbc; private string _itemCode; ...

  5. SSIS&colon;捕获修改了的数据

    获取修改了的数据一般有三种方式: 1.使用一个datetime列 缺点:是并不是每个表都会有个‘修改日期’字段来让你判断行是否修改过 使用实例可以参考我之前的文章:SSIS: 使用最大ID和最大日期来 ...

  6. NOIP2015酱油记

    day0 坐动车到广州..下午就在酒店颓... day1 早上6:30起床...大概8:00到六中..ZSJZ众貌似很晚才到..毕竟他们酒店就在学校门口(真的就刚刚好是门口...),大概8:15进去机 ...

  7. OpenCV-Python-边缘检测

    Canny边缘检测方法被誉为边缘检测的最优方法. import cv2 import numpy as np img = cv2.imread('handwriting.jpg', 0) edges ...

  8. sticky

    最近有点忘了position几个取值的内容,在这里简单总结一下. position的含义是指定位类型,取值类型可以有:static.relative.absolute.fixed.inherit和st ...

  9. windows server 2012 R2 远程桌面授权模式尚未配置

    windows server 2012 R2 远程桌面授权模式尚未配置,远程桌面服务将在120天内停止工作.如何破解这个宽限期,目前企业7位协议号码均不包含2012 R2以上授权. 那么只能蛋疼的“破 ...

  10. 转---30 分钟学会 Flex 布局

    正文从这开始- Flex 基本概念: 在 flex 容器中默认存在两条轴,水平主轴(main axis) 和垂直的交叉轴(cross axis),这是默认的设置,当然你可以通过修改使垂直方向变为主轴, ...