这道题用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 最大流的更多相关文章

  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 - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

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

  3. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

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

  4. POJ 1273 Drainage Ditches | 最大流模板

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

  5. POJ 1273 Drainage Ditches(最大流Dinic 模板)

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

  6. poj 1273 Drainage Ditches(最大流)

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

  7. POJ 1273 Drainage Ditches (网络最大流)

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

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

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

  9. POJ 1273 Drainage Ditches(网络流,最大流)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

随机推荐

  1. Codeforces985E. Pencils and Boxes (单调队列)

    题意:n个数 把他们放进一些盒子里 每个盒子最少放k个数 且最小和最大的差不能大于d 题解:显然排个序 对于当前点 存一下前面有哪些节点可以当作结尾 那么就可以枚举这些点的下一个点作为起点能否和当前点 ...

  2. C#服务端通过Socket推送数据到Android端App中

    需求: 描述:实时在客户端上获取到哪些款需要补货. 要求: 后台需要使用c#,并且哪些需要补货的逻辑写在公司框架内,客户端采用PDA(即Android客户端 版本4.4) . 用户打开了补货通知页面时 ...

  3. 洛谷——P2171 Hz吐泡泡

    P2171 Hz吐泡泡 题目描述 这天,Hz大大心血来潮,吐了n个不同的泡泡玩(保证没有重复的泡泡).因为他还要写作业,所以他请你帮他把这些泡泡排序成树(左子树<=根<右子树).输出它的后 ...

  4. docker 部署spring.boot项目【一】(引用外部配置文件)

    上一篇随笔,nginx是启动运行在容器内,spring.boot的web项目是运行在宿主内,这一篇的目的,是把web项目也制作成镜像,然后在容器里启动. 文件目录结构如下: 主要文件结构说明:(1)b ...

  5. oracle的分号、斜杠和commit

    ;分号表示一个语句的结束 //表示执行前面的一个代码块,例如begin/end,代码块后面必须跟/才能执行. commitcommit表示提交一个事务,例如insert,delete,update等, ...

  6. 简述systemd的新特性及unit常见类型分析、使用systemd管理编译安装的nginx

    1. systemd新特性 并行处理(同时启动)所有服务. 基于依赖关系定义的服务控制逻辑 系统状态快照 按需激活进程,只有第一次被访问时才会真正启动: 2. systemd的常见unit类型 Ser ...

  7. linux more-显示文件内容,每次显示一屏

    博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作.more名单中 ...

  8. CCF201612-2 工资计算 java(100分)

    试题编号: 201612-2 试题名称: 工资计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假 ...

  9. 关于js中的事件委托小案例

    需求:页面上有一个按钮,和一个空的ul,要求点击按钮,会给ul中动态添加li元素,然后,点击动态添加的元素,在控制台上输出,这是第几个元素 <ul> </ul> <but ...

  10. POJ 1811 大整数素数判断 Miller_Rabin

    #include <cstdio> #include <cstring> #include <cmath> #include <ctime> #incl ...