这道题用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. block教程

    http://pernghh.pixnet.net/blog/trackback/eac87d412e/33563409 本文来自台湾的某开发人员的博客,被墙,感觉讲的比较易懂,所以引过来.文字简体化 ...

  2. ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

    例子: Post p = new Post(); p.setPostId(3); p.setPostName("技术"); 在执行数据保持时提示session.save(p); 的 ...

  3. DH密钥交换算法

    DH密钥交换算法:DH的全称为Diffie-Hellman ,该算法可以在需要安全传输的前提下,确定双方的对称密钥,该算法的核心在于双方的私钥没有进入网络传输流程,根据对方的公钥和己方的私钥,可以计算 ...

  4. Luogu P3797 妖梦斩木棒

    解题思路 用线段树做这个就不用说了吧,但是要维护的东西确实很神奇.在每一个节点上都维护一个$lbkt$,表示这个区间上最靠左的右括号的位置:一个$rbkt$,表示这个区间上最靠右的左括号的位置.还有一 ...

  5. ubuntu14.04 fcitx安装

    先卸载ibus sudo apt-get remove ibus (也可尝试不卸载ibus,直接安装fcitx) 添加源 sudo add-apt-repository ppa:fcitx-team/ ...

  6. FZU 1492 地震预测(模拟链表的应用)(Java实现)

    FZU 1492 地震预测(模拟链表的应用)(Java实现) 怀特先生是一名研究地震的科学家,最近他发现如果知道某一段时间内的地壳震动能量采样的最小波动值之和,可以有效地预测大地震的发生. 假设已知一 ...

  7. js中细小点

    昨天在写前端代码的时候,遇到一个神奇的问题,我判断序号 num 和数组 arr 的 length 是否相等,如果相等则做想要的处理,伪码如下; if (num === arr.length) { fu ...

  8. 【Codeforces 501C】Misha and Forest

    [链接] 我是链接,点我呀:) [题意] 给你一棵树 但是每个节点只告诉你出度个数 以及所有和它相连的点的异或和. 让你还原这棵树 [题解] 叶子节点的话,他所有节点的异或和就是它那唯一的一个爸爸 因 ...

  9. Ubuntu 16.04下Markdown编辑器Haroopad

    1.下载deb包 地址:https://bitbucket.org/rhiokim/haroopad-download/downloads/haroopad-v0.13.2-x64.deb 这里是历史 ...

  10. url处理函数

    function UrlOption(url) { this.url = url || ''; this.init(); this.change = function (url) { this.url ...