poj 1273 Drainage Ditches_最大流模版
#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_最大流模版的更多相关文章
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- POJ 1273 Drainage Ditches 最大流
这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...
- POJ 1273 Drainage Ditches | 最大流模板
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- POJ 1273 Drainage Ditches(最大流Dinic 模板)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
随机推荐
- java 8 中lambda表达式学习
转自 http://blog.csdn.net/renfufei/article/details/24600507 http://www.jdon.com/idea/java/10-example-o ...
- AC大牛经典语录
超经典: 1. 为了世界的和平,为了女生的安全,我拼命做题,做题,做题! 2. A ac a day, keeps the doctor away! 3. from good to great ...
- 棋盘覆盖(大数阶乘,大数相除 + java)
棋盘覆盖 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在一个2k×2k(1<=k<=100)的棋盘中恰有一方格被覆盖,如图1(k=2时),现用一缺角的 ...
- linux下php扩展curl的安装
方法一 安装cURL wget http://curl.haxx.se/download/curl-7.17.1.tar.gz tar -zxf curl-7.17.1.tar.gz ./config ...
- NET基础课--XML基础
id level是属性,title为子节点 接下来操作子节点 比较xml的处理方式: 1和3适合数据量小的时候.
- WinForm RDLC SubReport Step by step
最近在做的一个PO管理系统,因为要用到订单打印,没有用水晶报表,直接使用VS2010的Reporting.参考了网上的一些文章,但因为找到的数据是用于WebForm的,适配到WinForm有点区别,竟 ...
- 自动匹配HTTP请求中对应实体参数名的数据(性能不是最优)
/// <summary> /// 获取请求参数字段 /// </summary> /// <typeparam name="T"></t ...
- Html5 代码
随着HTML5的流行,许多网站开始介绍HTML5元素和属性的用法,以及各种教程,并且越来越多老的浏览器开始兼容HTML5. 本文作者编译了10段非常实用的HTML5代码片段,开发者可以直接拿过去使 ...
- 自定义view(自定义view的时候,三个构造函数各自的作用)
package com.timeshare.tmband.Utils; import android.content.Context; import android.content.res.Typed ...
- java学习之部分笔记2
1.变量 实例变量和局部变量 实例变量系统会自动初始化为0和null(string),局部变量必须设定初始值. 静态方法里只能引用静态变量 数据类型的自动转换! int—>long 2.构造方法 ...