HD1532Drainage Ditches(最大流模板裸题 + 邻接表)
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13273 Accepted Submission(s): 6288
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
#include <iostream>
#include <queue>
#include<string.h>
using namespace std;
#define arraysize 201
int maxData = 0x7fffffff;
int capacity[arraysize][arraysize]; //记录残留网络的容量
int flow[arraysize]; //标记从源点到当前节点实际还剩多少流量可用
int pre[arraysize]; //标记在这条路径上当前节点的前驱,同时标记该节点是否在队列中
int n,m;
queue<int> myqueue;
int BFS(int src,int des)
{
int i,j;
while(!myqueue.empty()) //队列清空
myqueue.pop();
for(i=;i<m+;++i)
{
pre[i]=-;
}
pre[src]=;
flow[src]= maxData;
myqueue.push(src);
while(!myqueue.empty())
{
int index = myqueue.front();
myqueue.pop();
if(index == des) //找到了增广路径
break;
for(i=;i<m+;++i)
{
if(i!=src && capacity[index][i]> && pre[i]==-)
{
pre[i] = index; //记录前驱
flow[i] = min(capacity[index][i],flow[index]); //关键:迭代的找到增量
myqueue.push(i);
}
}
}
if(pre[des]==-) //残留图中不再存在增广路径
return -;
else
return flow[des];
}
int maxFlow(int src,int des)
{
int increasement= ;
int sumflow = ;
while((increasement=BFS(src,des))!=-)
{
int k = des; //利用前驱寻找路径
while(k!=src)
{
int last = pre[k];
capacity[last][k] -= increasement; //改变正向边的容量
capacity[k][last] += increasement; //改变反向边的容量
k = last;
}
sumflow += increasement;
}
return sumflow;
}
int main()
{
int i,j;
int start,end,ci;
while(cin>>n>>m)
{
memset(capacity,,sizeof(capacity));
memset(flow,,sizeof(flow));
for(i=;i<n;++i)
{
cin>>start>>end>>ci;
if(start == end) //考虑起点终点相同的情况
continue;
capacity[start][end] +=ci; //此处注意可能出现多条同一起点终点的情况
}
cout<<maxFlow(,m)<<endl;
}
return ;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
const int MAX = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
};
int n,m;
vector<Edge> edge;
vector<int> g[MAX];
int a[MAX],p[MAX];
void init()
{
for(int i = ; i <= m; i++)
g[i].clear();
edge.clear();
}
int Maxflow(int s,int t)
{
int flow = ;
while(true)
{
memset(a,,sizeof(a));
queue<int> q;
q.push(s);
a[s] = INF;
while(q.size())
{
int x = q.front();
q.pop();
int len = g[x].size();
for(int i = ; i < len; i++)
{
Edge e = edge[ g[x][i] ];
if(a[e.to] == && e.cap > e.flow)
{
p[e.to] = g[x][i];
a[e.to] = min(a[x],e.cap - e.flow);
q.push(e.to);
}
}
if(a[t])
break;
}
if(a[t] == )
break;
for(int i = t; i != s; i = edge[ p[i] ].from)
{
edge[ p[i] ].flow += a[t];
edge[ p[i] ^ ].flow -= a[t];
}
flow += a[t];
}
return flow;
}
int main()
{
while(scanf("%d%d", &n,&m) != EOF)
{
int s,e,v,t;
init();
for(int i = ; i <= n; i++)
{
scanf("%d%d%d",&s,&e,&v);
edge.push_back(Edge(s,e,v,));
edge.push_back(Edge(e,s,,));
t = edge.size();
g[s].push_back(t - );
g[e].push_back(t - );
} printf("%d\n",Maxflow(,m));
}
}
HD1532Drainage Ditches(最大流模板裸题 + 邻接表)的更多相关文章
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- 三种邻接表存图模板:vector邻接表、数组邻接表、链式前向星
vector邻接表: ; struct Edge{ int u,v,w; Edge(int _u=0,int _v=0,int _w=0){u=_u,v=_v,w=_w;} }; vector< ...
- 算法模板——sap网络最大流 3(递归+邻接表)
实现功能:同前 程序还是一如既往的优美,虽然比起邻接矩阵的稍稍长了那么些,不过没关系这是必然,但更重要的一个必然是——速度将是一个质的飞跃^_^(这里面的point指针稍作了些创新——anti指针,这 ...
- POJ 1273 Drainage Ditches | 最大流模板
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- 邻接表(C++)
adj_list_network_edge.h // 邻接表网边数据类模板 template <class WeightType> class AdjListNetworkEdge { p ...
- hdu 1532 Drainage Ditches(最大流模板题)
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)
最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...
- hdu Flow Problem (最大流 裸题)
最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
随机推荐
- Unity 协程Coroutine综合测试
using UnityEngine; using System.Collections; using System.Text; public class rotCube : MonoBehaviour ...
- ajax中加上AntiForgeryToken防止CSRF攻击
经常看到在项目中ajax post数据到服务器不加防伪标记,造成CSRF攻击 在Asp.net Mvc里加入防伪标记很简单在表单中加入Html.AntiForgeryToken()即可. Html.A ...
- 使用Spring.net中对Ado.net的抽象封装来访问数据库
使用Spring.net中对Ado.net的抽象封装来访问数据库 Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入 ...
- Anaroid WebView 的属性汇总
1. 打开网页时不调用系统浏览器, 而是在本WebView中显示: mWebView.setWebViewClient(new WebViewClient(){ @Override public bo ...
- C中的预编译宏定义
可以用宏判断是否为ARC环境 #if _has_feature(objc_arc) #else //MRC #endif C中的预编译宏定义 -- 作者: infobillows 来源:网络 在将一 ...
- VS2010报错无法编译:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid
win7 64位 专业版 + vs2010 从vc6.0下转过来的一个项目,突然遇到这个问题. 解决方案: 用C:\Windows\winsxs\x86_netfx-cvtres_for_vc_and ...
- IT男的”幸福”生活
IT男的”幸福”生活 IT男的”幸福”生活"续1 IT男的”幸福”生活"续2 IT男的”幸福”生活"续3 IT男的”幸福”生活"续4 IT男的”幸福”生活 ...
- 第七章 new的三步曲
这章是本系列文章的重点,这章揭示了js对象的真正本质 看下面的事例 var a = new b(); 等价于 ①var a={}; ②a.__proto__=b.prototype; ③b.call( ...
- Orchard创建自定义表单
本文链接:http://www.cnblogs.com/souther/p/4520130.html 主目录 自定义表单模块可以用来获取网站前台用户的信息.自定义表单需要与一个内容类型结合使用.它可以 ...
- 最简单的 Web Service 入门 (看了包会)
原理:WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于SOAP协议的网络应用间的交互. 作用:主要 ...