POJ 1273 (基础最大流) Drainage Ditches
虽然算法还没有理解透,但以及迫不及待地想要A道题了。
非常裸的最大流,试试lrj的模板练练手。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; const int maxn = + ;
const int INF = ; 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, M;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn]; //到起点i的可改进量
int p[maxn]; //最短路数上p的入弧编号 void Init(int n)
{
for(int i = ; i < n; ++i) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , )); //反向弧
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} int MaxFlow(int s, int t)
{
int flow = ;
for(;;)
{
memset(a, , sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = INF;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); ++i)
{
Edge& e = edges[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 u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
}
flow += a[t];
}
return flow;
} int main()
{
freopen("in.txt", "r", stdin); while(scanf("%d%d", &M, &n) == )
{
Init(n);
int u, v, c;
for(int i = ; i < M; ++i)
{
scanf("%d%d%d", &u, &v, &c);
AddEdge(u-, v-, c);
}
printf("%d\n", MaxFlow(, n-));
} return ;
}
代码君
POJ 1273 (基础最大流) Drainage Ditches的更多相关文章
- Poj(1273),最大流,EK
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 69355 Accepted: 2687 ...
- POJ 1273 网络流(最大流)模板
http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...
- 网络流 最大流 Drainage Ditches Dinic
hdu 1532 题目大意: 就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速,本题就是让你求出最大流速,无疑要运用到求最大流了 ...
- POJ 2516 基础费用流
题意 有n个顾客,m个供应商,k种货物,给你顾客对于每种货物的要求个数,和供应商对于每种货物的现有量,以及供应每种货物的时候供应商和顾客之间的运输单价,问你满足所有顾客的前提下的最小运输费 ...
- poj 2135 (基础费用流)
题意:从1到n再到1,每条边只能走一次,求最短距离. 建图:每条边只能走一次就是流量是1,添加源点与1相连,容量为2,费用为0,n与汇点相连容量为2,费用为0: 求增广路用SPFA最短路求,, #in ...
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
随机推荐
- PHP通过IP 获取 地理位置(实例代码)
发布:JB02 来源:脚本学堂 分享一例php代码,实现通过IP地址获取访问者的地理位置,在php编程中经常用到,有需要的朋友参考下吧.本节内容:PHP通过IP获取地理位置 例子: 复制代码代码示 ...
- DataSnap Demo:TFDConnection、最大连接数、客户端回叫功能、多线程模拟、压力测试等
一.限制最大连接数,并验证来访者用户身份: procedure TServerContainer1.DSServer1Connect( DSConnectEventObject: TDSConnect ...
- PL/SQL Developer编码格式设置及中文乱码解决方案
1.PL/SQL Developer中文字段显示乱码 原因:因为数据库的编号格式和pl /sql developer的编码格式不统一造成的. 2.PL/SQL Developer编码格式设置详细的解决 ...
- .NET基础之集合
集合可以说是数组的超集,集合可以维护对象数组,集合包含了更高级的功能.例如控制对其包含的对象的访问.搜索和排序等.数组是固定的,一旦我们创建好了数组,不能在现有数组的末尾添加新项,除非我们创建新的数组 ...
- WPF中让TextBlock每一个字符显示不同的颜色
XAML代码: <TextBlock x:Name="tb"> <Run Foreground="Red">R</Run> ...
- Java 动态代理(转载)
JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后 处理消息等.代理类与 ...
- There is a legend about a bird
There is a legend about a bird which sings just once in its life, more sweetly than any other ...
- 进程、线程、GDI+、XML、委托
进制 表示某一位置上的数运算时是逢X进一位.二进制就是逢二进一, 十进制是逢十进一,十六进制是逢十六进一,以此类推. so:二进制001010101只有0和1计算机中的数据都是二进制表示,四进制以0. ...
- 使用 Android Studio 跑新浪微博SDK Demo遇到的问题及解决
概述 这是新浪微博官方 Android SDK Demo 使用 Android Studio 导入.编译并运行通过的版本. 源码:WeiboSdkDemo 官方项目请点击: weibo_android ...
- bnuoj 4225 杨辉三角形(规律)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4225 [题意]: 给定任意杨辉三角的行数n,请输出杨辉三角中前n行中总共有多少偶数. [题解]: 找 ...