Drainage Ditches---hdu1532(最大流, 模板)
题目链接: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(最大流, 模板)的更多相关文章
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj 1532 Drainage Ditches【最大流模板题】
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj1273 Drainage Ditches (最大流模板)
http://poj.org/problem?id=1273 Dinic算法 这是一道最大流的经典题 最大流尽量应该用边表,优于邻接矩阵(所以我写了邻接矩阵版的之后又写了个边表) 用了新学的Dinic ...
- 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) ...
- poj 1273 (nyoj 323) Drainage Ditches : 最大流
点击打开链接 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49648 Accepte ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- HD1532Drainage Ditches(最大流模板裸题 + 邻接表)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
随机推荐
- Redis简单介绍
redis简单介绍 Redis VS key-value缓存产品 Redis支持数据的持久化,能够将内存中的数据保持在磁盘中,重新启动的时候能够再次载入进行使用. Redis不只支持简单的key-va ...
- 安装expect命令 两种方式
yum安装 yum -y install expect 手动安装 expect以及tcl版本 #!/bin/bash oldpath=`pwd` tar -zxf tcl8.4.20-src.tar. ...
- 学习PHP垃圾回收机制了解引用计数器的概念
php变量存在一个叫"zval"的变量容器中,"zval"变量容器包括含变量的类型和值,还包括额外的两个字节信息,分别是“is_ref”表示变量是否属于引用,“ ...
- redis-stat 安装
apt-get install ruby apt-get install rubygems redis-stat安装: cd/root git clone https://github.com ...
- librtmp将本地FLV文件发布到RTMP流媒体服务器
没有用到ffmpeg库 可以将本地FLV文件发布到RTMP流媒体服务器 使用librtmp发布RTMP流可以使用两种API:RTMP_SendPacket()和RTMP_Write(). 使用RTMP ...
- 数学 + 带权中位数 - SGU 114 Telecasting station
Telecasting station Problem's Link Mean: 百慕大的每一座城市都坐落在一维直线上,这个国家的政府决定建造一个新的广播电视台. 经过了许多次试验后,百慕大的科学家们 ...
- php juery ajax 传值
<script src="__PUBLIC__/jquery-1.11.2.min.js" type="text/javascript"></ ...
- 第二百五十八节,Tornado框架-逻辑处理get()方法和post()方法,初识模板语言
Tornado框架-逻辑处理get()方法和post()方法,初识模板语言 Tornado框架,逻辑处理里的get()方法,和post()方法 get()方法,处理get方式的请求post()方法,处 ...
- JDBC中,用于表示数据库连接的对象是。(选择1项)
JDBC中,用于表示数据库连接的对象是.(选择1项) A.Statement B.Connection C. DriverManager D.PreparedStatement 解答:B
- 【BZOJ】1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=1681 太裸了.. #include <cstdio> #include <cstr ...