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 ...
随机推荐
- 文件操作之格式化IO
其实在我使用最多的文件操作中,还是喜欢格式化IO控制的方式,简单方便易理解. #include <stdio.h> #include<stdlib.h> int main() ...
- WP架构设计(一)MVVM回顾
[MVVM的定义] MVVM的目的是什么? 简单总结起来一句话:分离UI逻辑和业务逻辑.这一点和被大家熟知的MVP和MVC是一致的. 下面详细来说明下这个问题,下面一段英文来自Msdn ...
- openresty package.path require 报错
在文件中 package.path = '/usr/local/share/lua/5.1/?.lua;/usr/local/openresty/lualib/resty/?.lua;' packag ...
- C/C++基础问题归集
include " ":先从本地目录,后从系统路径include <>: 先从系统路径,后从本地目录 一般用哪个都没关系,只是速度有差别罢了
- 008Maven_建立私服
参考博客:http://blog.csdn.net/fengspg/article/details/22507737 .1. Maven私服 关于中央仓库注意事项 l 地址: 目前来说: htt ...
- Cloudera公司主要提供Apache Hadoop开发工程师认证
Cloudera Cloudera公司主要提供Apache Hadoop开发工程师认证(Cloudera CertifiedDeveloper for Apache Hadoop ,CCDH)和Apa ...
- 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)
http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...
- 透過 bc 計算 pi
echo "scale=${num}; 4*a(1)" | bc -lq例如: echo "scale=5000; 4*a(1)" | bc -lq 4*a(1 ...
- 1 week110的zookeeper的安装 + zookeeper提供少量数据的存储
随时查看,zookeeper企业里公认的最新文档版本! https://archive.apache.org/dist/ 下面是在weekend110上的zookeeper的安装 在 ...
- uva 11381(神奇的构图、最小费用最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29821 思路:首先拆点,每个字符对应的位置拆成i和i+len,然后 ...