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 ...
随机推荐
- 牛客多校Round 4
Soved:3 rank:133 A.Ternay String 欧拉降幂一下 但是反复求phi会超时 但mod是同一个就可以记忆化一下 #include <bits/stdc++.h> ...
- TWaver矢量小试——Android演进路线图
还有半个多月就到春节了,年底相信很多公司都会进行年度总结以及公司发展状况总结,在这过程中难免会用到RoadMap,在这我们也使用TWaver的矢量部分绘制一个Android系统的发展历程.先看效果:什 ...
- tarjan求强连通分量模板
什么是强连通分量? 百度百科 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(stro ...
- java.lang unsupported classversion解决方法
设置编译的jdk和运行的jdk环境版本是否一致.一般都是jdk导致的.刚开始用jdk1.6编译运行,死活不行,换成jdk1.7运行也是1.7,ok
- Python-函数和代码复用
函数的定义与使用 >函数的理解与定义 函数是一段代码的表示 -函数是一段具有特定功能的.可重用的语句组 -函数是一种功能的抽象,一般函数表达特定功能 -两个作用:降低编程难度 和 代码复用 de ...
- software collection
software software Table of Contents 1. Privacy 2. GFW 2.1. google search 2.2. 修改 DNS 服务器 2.2.1. 修改ip ...
- Java基础学习总结(82)——Java泛型实例教程
1.为什么需要泛型 泛型在Java中有很重要的地位,网上很多文章罗列各种理论,不便于理解,本篇将立足于代码介绍.总结了关于泛型的知识.希望能给你带来一些帮助. 先看下面的代码: List list = ...
- codevs1099 字串变换
题目描述 Description 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ ...
- git远程上传文件
[第一步]建立先仓库 第一步的话看一般的提示就知道了,在github新建一个repository(谷歌可以解决),都是可视化的界面操作,所以难度不大.或者看这里:https://help.github ...
- 通过代码学习python之@property,@staticmethod,@classmethod
URL: https://www.the5fire.com/python-property-staticmethod-classmethod.html #coding=utf-8 class MyCl ...