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 ...
随机推荐
- 应用DataAdapter对象填充DataSet数据集
private void Form1_Load(object sender, EventArgs e) { string strCon = "Server=localhost;User Id ...
- NameNode机制和DataNode机制
首先我们看一下NAMENODE: 我们已经知道了NAMENODE作为DATANODE的管理者,其重要性不言而喻,那么NAMENODE是怎么管理数据的呢? 首先,我们看一下上面这张图,每次客户端读写数据 ...
- Cocos2d-x 3.0final 终结者系列教程07-画图节点Node
在Cocos2d-x中全部能看到的都是引擎调用底层图形库函数绘制完毕的. Cocos2d-x将屏幕全部要绘制的全部内容逻辑上保存到一个场景Scene中(尺寸通常会和屏幕大小一致) 而在Scene中又包 ...
- 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch
Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...
- treegrid-dnd.js
(function($){ $.extend($.fn.treegrid.defaults, { onBeforeDrag: function(row){}, // return false to d ...
- java.lang下面有一个接口:Comparable(可比较的)
对于自定义对象,Sort不知道规则,所以无法比较.这种情况下一定要定义排序规则.方式有两种: java.lang下面有一个接口:Comparable(可比较的) 可以让自定义对象实现一个接口,这个接口 ...
- JAVA 并发编程-多个线程之间共享数据(六)
多线程共享数据的方式: 1.假设每一个线程运行的代码同样.能够使用同一个Runnable对象,这个Runnable对象中有那个共享数据,比如,卖票系统就能够这么做. 2,假设每一个线程运行的代码不同. ...
- COCOS2D-X多层单点触摸分发处理方案?
如今的问题是点击button的时候,会触发底层的触摸事件,怎么不触发底层的触摸事件啊?
- 求出每个team粉丝数最多的3个国家
有这么个表 fans(team,nationality,fanCount) 'Barcelona','Germany',12000'Barcelona','Spain',18000'Barcelona ...
- log4j配置文件
log4j.rootLogger=INFO,CONSOLElog4j.addivity.org.apache=truelog4j.appender.stdout=org.apache.log4j.Co ...