pku1273 Drainage Ditches
http://poj.org/problem?id=1273
网络流,Dinic
#include <stdio.h>
#include <string.h>
#include <queue> using namespace std; const int inf = <<;
const int maxn = , maxm = ; struct Edge
{
int v, f, nxt;
}; int n, src, sink;
int g[maxn + ];
int nume;
Edge e[maxm* + ]; void addedge(int u, int v, int c)
{
e[++nume].v = v;
e[nume].f = c;
e[nume].nxt = g[u];
g[u] = nume;
e[++nume].v = u;
e[nume].nxt = g[v];
g[v] = nume;
} queue<int> que;
bool vis[maxn + ];
int dist[maxn + ]; void bfs()
{
memset(dist, , sizeof(dist));
while(!que.empty())
{
que.pop();
}
vis[src] = true;
que.push(src);
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i=g[u]; i; i=e[i].nxt)
{
if(e[i].f && !vis[e[i].v])
{
que.push(e[i].v);
dist[e[i].v] = dist[u] + ;
vis[e[i].v] = true;
}
}
}
} int dfs(int u, int delta)
{
if(u == sink)
{
return delta;
}
else
{
int ret = ;
for(int i=g[u]; delta && i; i=e[i].nxt)
{
if(e[i].f && dist[e[i].v] == dist[u] + )
{
int dd = dfs(e[i].v, min(e[i].f, delta));
e[i].f -= dd;
e[i ^ ].f += dd;
delta -= dd;
ret += dd;
}
}
return ret;
}
} int maxflow()
{
int ret = ;
while(true)
{
memset(vis, , sizeof(vis));
bfs();
if(!vis[sink])
{
return ret;
}
ret += dfs(src, inf);
}
} int main()
{
int n, m;
int i, x, y, z;
while(~scanf("%d%d", &m, &n))
{
memset(g, , sizeof(g));
nume = ;
src = ;
sink = n;
for(i=; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
}
x = maxflow();
printf("%d\n", x);
}
return ;
}
pku1273 Drainage Ditches的更多相关文章
- POJ 1273 Drainage Ditches题解——S.B.S.
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67823 Accepted: 2620 ...
- poj1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68414 Accepted: 2648 ...
- POJ 1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67387 Accepted: 2603 ...
- HDU1532 Drainage Ditches 网络流EK算法
Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- POJ 1273 Drainage Ditches -dinic
dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...
- Drainage Ditches(dinic)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59210 Accepted: 2273 ...
- Drainage Ditches
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Drainage Ditches 分类: POJ 图论 2015-07-29 15:01 7人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62016 Accepted: 23808 De ...
随机推荐
- Linux内核的整体架构
Linux内核的整体架构 作者:蜗蜗 发布于:2014-2-21 13:23 分类:Linux内核分析 原文:http://www.wowotech.net/linux_kenrel/11.html ...
- debian系统安装Thinkpad T410s的无线网卡驱动:centrino Advanced-N 6200 2x2 AGN
前几天搞到手一台小黑:T410s.自带系统是win7.由于想学习debian,所以就搞成了双系统,安装了一套debian 6.0. 可是让我困惑的是在debian下,无法使用T410s的网卡,因为默认 ...
- 【ACM】魔方十一题
0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...
- bigtint;int;smallint;tinyint
bigint对应的是Int64 [long] int对应的是Int32 [int] smallint对应的是Int16 [short] tinyint对应的是 [byte ...
- Effective STL 中文版(大全)
Effective STL 中文版(大全) 作者:winter 候捷说,对于STL,程序员有三个境界,开始是使用STL,然后是理解STL,最后是补充STL.Effective STL是一本非常好的书, ...
- Centos 6.5LAMP服务器(Apache+PHP+MySQL)的搭建
1.首先看下你的防火墙是否处于开启状态,如果是开启状态,按照如下方法来配置你的防火墙(如果你在安装虚拟机时就没有开启过防火墙,那么这一步就省略了): 1.配置防火墙,开启80端口.3306端口 vi ...
- laravel创建新model数据的两种方法
laravel中的CRUD操作中,通过对代表数据表中row的model对象操作,来更新数据库表. 对于创建新的row的操作,有两种功能上相同的方法: 1.create: $user = User::c ...
- KM算法(二分图的最佳完美匹配)
KM算法大概过程: (1)初始化Lx数组为该boy的一条权值最大的出边.初始化Ly数组为 0. (2)对于每个boy,用DFS为其找到一个girl对象,顺路记录下S和T集,并更新每个girl的slac ...
- python tile函数用法
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...
- linux shell中的 #!/bin/bash
#!/bin/bash是指此脚本使用/bin/bash来解释执行. 其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径. bash只是shell的一种,还有很多其它shell,如:sh ...