POJ 1273 Drainage Ditches 最大流
这道题用dinic会超时 用E_K就没问题 注意输入数据有重边。POJ1273
dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的。
然而dinic主要依靠Dfs寻找增广路,故而使用了太多次递归,而利用bfs寻找增广路(使用队列而不用递归)的EK等于用栈的方式实现了dinic的递归,所以 大幅提高了效率。
最大流算法的核心就是找到增广路并且增加反向边流量,理解了这个,最大流算法就很简单了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std; const int maxn=200*2,s=1,INF=1e+8; int t,n,m,cap[maxn][maxn],from[maxn],to[maxn],c[maxn],f[maxn],dep[maxn],cur[maxn];
vector<int>po[maxn]; bool bfs()
{
bool vis[maxn];
memset(vis,0,sizeof(vis));
vis[s]=true;
queue<int>q;
q.push(s);
dep[s]=0;
while(!q.empty())
{
int np=q.front();q.pop();
if(np==t)return true;
for(int i=0;i<po[np].size();i++)
{
int ne=po[np][i];
int next=to[ne];
if((vis[next])||(c[ne]<=f[ne]))continue;
vis[next]=true;
dep[next]=dep[np]+1;
q.push(next);
}
}
return vis[t];
}
int min( int a, int b)
{
if(a<b)return a;
else return b;
}
int dfs(int now, int flo)
{
if(now==t||flo==0)return flo;
int flow=0;
for(int i=cur[now];i<po[now].size();i++)
{
int ne=po[now][i];
int next=to[ne];
if(dep[next]!=dep[now]+1)continue;
if(c[ne]<=f[ne])continue;
long long int fd=dfs(next,min(flo,c[ne]-f[ne]));
f[ne]+=fd;
if(ne>=200)f[ne-200]-=fd;
else f[ne+200]-=fd;
flow+=fd;
flo-=fd;
if(flo==0)break;
cur[now]++;
}
return flow;
}
int d[maxn],fa[maxn];
void find()
{
memset(d,0,sizeof(d));
d[s]=INF;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=0;i<po[u].size();i++)
{
int ne=po[u][i];
int next=to[ne];
if(d[next]!=0||cap[u][next]<=0)continue;
d[next]=min(d[u],cap[u][next]);
q.push(next); fa[next]=u;
if(next==t)
{
while(!q.empty())q.pop();
return ;
}
}
}
}
int E_K()
{
int ans=0;
while(true)
{
find();
ans+=d[t];
if(d[t]==0)break;
for(int i=t;i!=s;i=fa[i])
{
cap[fa[i]][i]-=d[t];
cap[i][fa[i]]+=d[t];
}
}
return ans;
}
int main()
{freopen("t.txt","r",stdin);
ios::sync_with_stdio(false);
while(cin>>n>>m)
{ t=m;
memset(f,0,sizeof(f));memset(c,0,sizeof(c));memset(cap,0,sizeof(cap));
for(int i=0;i<n;i++)
{
cin>>from[i]>>to[i]>>c[i];po[from[i]].push_back(i);cap[from[i]][to[i]]+=c[i];//原边
from[i+200]=to[i];to[i+200]=from[i];po[to[i]].push_back(i+200);//反向边
}
cout<<E_K()<<endl;
}
return 0;
}
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 | 最大流模板
#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 ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
随机推荐
- centos7安装个人博客wordpress
第一步: 安装apache web 第二步: 安装MariaDB数据库 第三步: 安装PHP 第四步: 安装phpMyAdmin 第五部: 创建数据库: 第六部: 下载wordpress 第七部:复制 ...
- TestNG安装及配置
1. 在idea中新建一个maven项目 2. 在pom.xml中添加testng和reportng依赖 <dependencies> <!-- 添加testNG依赖 --> ...
- Jenkins 的插件AnsiColor 多颜色
- Greek symbols --Latex
$\propto$ \propto $\infty$ \infty $\ne$ \ne $\approx$ \approx $\sim$ : \sim --- same ...
- circumferential averge streamwise velocity using Tecplot and Matlab
Input: results from solver output: circumferential averge physical quantities( such as streamwise ve ...
- java 使用OpenOffice文件实现预览
1.安装OpenOffice软件 安装教程:https://jingyan.baidu.com/article/c275f6ba12c07ce33d756732.html 2.安装完成后,创建项目,p ...
- web项目的创建
1) 创建Mave的webapp项目 2) 在Pom文件中添加servlet-api的依赖 <dependency> <groupId>javax.servlet</gr ...
- java 生成20位唯一ID,生成不会重复的20位数字----https://blog.csdn.net/weixin_36751895/article/details/70331781
java 生成20位唯一ID,生成不会重复的20位数字----https://blog.csdn.net/weixin_36751895/article/details/70331781
- Modify MySQL dump file the fatest way
使用mysql命令导入mysqldump生成的sql文件时,为了提高导入速度,往往需要修改dump文件,但是面对一个几十GB的文件,这事儿就太崩溃了,最快速的方法是这么做: ( echo " ...
- NOIP2014 提高组合集
NOIP 2014 提高组 合集 D1 T1 生活大爆炸版石头剪刀布 首先,先将两个人的猜拳序列都变得不小于n.然后逐个模拟.胜败什么的看表就行了. #include <iostream> ...