题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532

最大流模板题;

EK:(复杂度为n*m*m);

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
#define N 220
int maps[N][N], pre[N], ans;
bool bfs(int s, int e)
{
memset(pre, , sizeof(pre));
queue<int>Q;
Q.push(s);
while(Q.size())
{
int i = Q.front();
Q.pop();
if(i == e)
return true;
for(int j=; j<=e; j++)
{
if(pre[j]== && maps[i][j] > )
{
pre[j] = i;
Q.push(j);
}
}
}
return false;
}
void EK(int s, int e)
{
while(bfs(s, e))
{
int Min = INF;
for(int i=e; i!=s; i=pre[i])
Min=min(maps[pre[i]][i], Min);
for(int i=e; i!=s; i=pre[i])
{
maps[pre[i]][i]-=Min;
maps[i][pre[i]]+=Min;
}
ans+=Min;
}
}
int main()
{
int n, m, x, y,c;
while(scanf("%d%d", &m, &n)!=EOF)
{
memset(maps, , sizeof(maps));
for(int i=; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &c);
maps[x][y] += c;
}
ans = ;
EK(, n);
printf("%d\n", ans);
}
return ;
}

Dinic:(复杂度为n*n*m)

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; #define N 220
#define INF 0xfffffff int n, ans, Head[N], cnt, Layer[N];
struct Edge
{
int v, flow, next;
} e[*N]; void Add(int u, int v, int flow)
{
e[cnt].v = v;
e[cnt].flow = flow;
e[cnt].next = Head[u];
Head[u] = cnt++;
}
bool bfs(int S, int E)
{
memset(Layer, , sizeof(Layer));
Layer[S] = ;
queue<int>Q;
Q.push(S);
int p, q;
while(!Q.empty())
{
p = Q.front();
Q.pop();
if(p == E)return true;
for(int i=Head[p]; i!=-; i=e[i].next)
{
q = e[i].v;
if(!Layer[q] && e[i].flow)
{
Layer[q] = Layer[p]+;
Q.push(q);
}
}
}
return false;
}
int dfs(int u, int MaxFlow, int E)
{
if(u == E)return MaxFlow;
int uflow=;
for(int i=Head[u]; i!=-; i=e[i].next)
{
int v = e[i].v;
if(Layer[v]==Layer[u]+ && e[i].flow)
{
int flow = min(e[i].flow, MaxFlow - uflow);
flow = dfs(v, flow, E); e[i].flow -= flow;
e[i^].flow += flow;
uflow += flow;
if(uflow==MaxFlow)break;
}
}
if(uflow==)
Layer[u]=;
return uflow;
}
void Dinic()
{
while(bfs(, n))
{
ans+=dfs(, INF, n);
}
}
int main()
{
int a, b, flow, m;
while(scanf("%d%d", &m, &n)!=EOF)
{
memset(Head, -, sizeof(Head));
cnt = ;
for(int i=; i<=m; i++)
{
scanf("%d%d%d", &a, &b, &flow);
Add(a, b, flow);
Add(b, a, );
}
ans = ;
Dinic();
printf("%d\n", ans);
}
return ;
}

Drainage Ditches---hdu1532(最大流, 模板)的更多相关文章

  1. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. hdoj 1532 Drainage Ditches【最大流模板题】

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. poj1273 Drainage Ditches (最大流模板)

    http://poj.org/problem?id=1273 Dinic算法 这是一道最大流的经典题 最大流尽量应该用边表,优于邻接矩阵(所以我写了邻接矩阵版的之后又写了个边表) 用了新学的Dinic ...

  4. POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. poj 1273 (nyoj 323) Drainage Ditches : 最大流

    点击打开链接 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49648   Accepte ...

  6. poj 1273 Drainage Ditches(最大流)

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

  7. poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53640   Accepted: 2044 ...

  8. HDU 1532||POJ1273:Drainage Ditches(最大流)

    pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  9. HD1532Drainage Ditches(最大流模板裸题 + 邻接表)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

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

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

随机推荐

  1. Webpack实例教程及模块化规范

    Webpack 是当下最热门的前端资源模块化管理和打包工具.它能够将很多松散的模块依照依赖和规则打包成符合生产环境部署的前端资源. 通过 loader 的转换,不论什么形式的资源都能够视作模块,比方 ...

  2. C#string数组转换到int数组并得到最大最小值

    string[] input = { "1", "2", "3", "4", "5", " ...

  3. C++ 类的对象管理模型初讲

    //类的对象管理模型初讲 #include<iostream> using namespace std; class PointA{ private: int x;//占据4个字节大小的内 ...

  4. Python安装相关的机器学习库以及图像处理库

    安装 sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-m ...

  5. 匿名内部类 Inner class

    先说结论 匿名内部类分两种,一种是接口的匿名实现,一种是类的匿名子类!后者往往用于修改特定方法. 再说起因 本来以为匿名内部类很简单,就是接口的匿名实现,直到我发现了下面这段代码: public cl ...

  6. OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言

    OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(ExpressionLanguage,简称为EL),通过它简单一致的表达式语法,可以存取 ...

  7. thinkphp相关功能整合系列

    thinkphp整合系列之短信验证码.订单通知 thinkphp整合系列之rbac的升级版auth权限管理系统demo thinkphp整合系列之阿里云oss thinkphp整合系列之phpmail ...

  8. ThinkPHP项目笔记之RBAC(权限)下篇

    接着谈谈:添加用户以及用户管理列表 e.添加用户

  9. Wamp2.5 64bit,无法改动MySQL datadir位置

    今天偶然想到去更新一下机子里面PHP的版本号,然后又一次去wamp官网下载了WAMP(wamp 64  Apache : 2.4.9 MySQL : 5.6.17 PHP : 5.5.12 PHPMy ...

  10. 选择LoadRunner Protocol的两大定律

    选择LoadRunner Protocol的两大定律 确定性能测试脚本录制时使用的协议类型经常是一个容易引起误会的问题.很多刚刚接触到性能测试的同行常常会想当然地根据开发语言等来决定协议的选取,导致录 ...