题目链接: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. CodeForces 558D

     Guess Your Way Out! II Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. Python操作Word【批量生成文章】

    http://www.cnblogs.com/codex/p/4668396.html 需要做一些会议记录.总共有多少呢?五个地点x7个月份x每月4篇=140篇.虽然不很重要,但是140篇记录完全雷同 ...

  3. H2 Database 支持数据类型

    整数(INT) -2147483648 到 2147483647 java.lang.Integer 布尔型(BOOLEAN) TRUE 和 FALSE java.lang.Boolean 微整数(T ...

  4. SenCha Touch 与 EXTJS 安装Myeclipse 插件

    http://www.cnblogs.com/jirimutu01/default.html 关于SenchaEclipsePlugin插件的安装和使用 使用过eclipse开发java程序的人都知道 ...

  5. 扫描线 - UVALive - 6864 Strange Antennas

    Strange Antennas Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=87213 M ...

  6. Python 矩阵与矩阵以及矩阵与向量的乘法

    import numpy as np numpy模块的array相乘时,有两种方式:一是矩阵形式,二是挨个相乘. 需要用矩阵形式相乘时,则要用np.dot()函数. #矩阵与矩阵相乘a = np.ar ...

  7. 011杰信-创建购销合同Excel报表系列-4-建立合同货物(修改,删除):合同货物表是购销合同表的子表

    前面的一篇文章做的是修改删除,这篇文章做的是合同货物的修改和删除. 业务功能如下:

  8. 基于struts2框架文件的上传与下载

    在开发一些社交网站时,需要有允许用户上传自己本地文件的功能,则需要文件的上传下载代码. 首先考虑的是文件的储存位置,这里不考虑存在数据库,因为通过数据库查询获取十分消耗资源与时间,故需将数据存储在服务 ...

  9. 理解Loadrunner中的Browser Emulation Simulate

    案例 测试环境描述: 客户端 5台 Windows2000机器.服务器端  20台机器 一台F5(负载均衡设备,提供一个唯一的IP供客户端访问) 客户端绑定Host后,使用域名http://www.* ...

  10. JAVA增删改查XML文件

    最近总是需要进行xml的相关操作. 不免的要进行xml的读取修改等,于是上网搜索,加上自己的小改动,整合了下xml的常用操作. 读取XML配置文件 首先我们需要通过DocumentBuilderFac ...