Drainage Ditches(POJ1273+网络流+Dinic+EK)
题目:


题意:求最大流。
思路:测板子题,分别用Dinic和EK实现(我的板子跑得时间均为0ms)。
Dinic代码实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, tot, maxflow, s, t, u, v, w;
int head[maxn<<], d[maxn<<]; queue<int> q; struct edge {
int v, w, next;
}ed[maxn<<]; void addedge(int u, int v, int w) {
ed[tot].v = v;
ed[tot].w = w;
ed[tot].next = head[u];
head[u] = tot++;
ed[tot].v = u;
ed[tot].w = ;
ed[tot].next = head[v];
head[v] = tot++;
} bool bfs() {
memset(d, , sizeof(d));
while(!q.empty()) q.pop();
q.push(s);
d[s] = ;
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = head[x]; ~i; i = ed[i].next) {
if(ed[i].w && !d[ed[i].v]) {
q.push(ed[i].v);
d[ed[i].v] = d[x] + ;
if(ed[i].v == t) return ;
}
}
}
return ;
} int dinic(int x, int flow) {
if(x == t) return flow;
int rest = flow, k;
for(int i = head[x]; ~i && rest; i = ed[i].next) {
if(ed[i].w && d[ed[i].v] == d[x] + ) {
k = dinic(ed[i].v, min(rest, ed[i].w));
if(!k) d[ed[i].v] = ;
ed[i].w -= k;
ed[i^].w += k;
rest -= k;
}
}
return flow - rest;
} int main() {
//FIN;
while(~scanf("%d%d", &m, &n)) {
s = , t = n;
tot = , maxflow = ;
memset(head, -, sizeof(head));
for(int i = ; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
int flow = ;
while(bfs()) {
while(flow = dinic(s, inf)) {
maxflow += flow;
}
}
printf("%d\n", maxflow);
}
return ;
}
EK实现如下:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, m, u, v, w, tot, maxflow, s, t;
int head[maxn<<], vis[maxn], incf[maxn], pre[maxn]; struct edge {
int v, w, next;
}ed[maxn<<]; void addedge(int u, int v, int w) {
ed[tot].v = v;
ed[tot].w = w;
ed[tot].next = head[u];
head[u] = tot++;
ed[tot].v = u;
ed[tot].w = ;
ed[tot].next = head[v];
head[v] = tot++;
} bool bfs() {
memset(vis, , sizeof(vis));
queue<int> q;
q.push(s);
vis[s] = ;
incf[s] = inf;
while(!q.empty()) {
int x = q.front();
q.pop();
for(int i = head[x]; i != -; i = ed[i].next) {
if(ed[i].w) {
int v = ed[i].v;
if(vis[v]) continue;
incf[v] = min(incf[x], ed[i].w);
pre[v] = i;
q.push(v);
vis[v] = ;
if(v == t) return ;
}
}
}
return ;
} void update() {
int x = t;
while(x != s) {
int i = pre[x];
ed[i].w -= incf[t];
ed[i^].w += incf[t];
x = ed[i^].v;
}
maxflow += incf[t];
} int main() {
//FIN;
while(~scanf("%d%d", &m, &n)) {
s = , t = n;
tot = , maxflow = ;
memset(head, -, sizeof(head));
memset(pre, -, sizeof(pre));
memset(incf, , sizeof(incf));
for(int i = ; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
while(bfs()) update();
printf("%d\n", maxflow);
}
return ;
}
Drainage Ditches(POJ1273+网络流+Dinic+EK)的更多相关文章
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- POJ1273 Drainage Ditches (网络流)
Drainage Ditches Time Limit: 1000MS Memor ...
- POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ_1273 Drainage Ditches 【网络流】
一.题面 Drainage Ditches 二.分析 网络流的裸题. 1 Edmonds-Karp算法 求解网络流其实就是一个不断找增广路,然后每次找到一条增广路后更新残余网络的一个过程. EK算法主 ...
- HDU 1532 Drainage Ditches (网络流)
A - Drainage Ditches Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1532 Drainage Ditches (最大网络流)
Drainage Ditches Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) To ...
- 网络流dinic ek模板 poj1273
这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...
随机推荐
- Python 元组 集合
1. 元组 >>> a = (1,2,3,4,5) >>> b = list(a) #转换成列表对象, 可以更改 >>> b [1, 2, 3, ...
- TCP系列37—Keep Alive—1、TCP存活检测
一.TCP存活(keepalive)检测的背景 对于TCP设计来说,如果一个客户端和服务器端建立连接后,不在进行数据传输,那么这个连接将会一直存在下去,理论上即使中间的路由器崩溃重启.或者中间的网络线 ...
- php添加扩展 在phpinfo能看到该扩展,但在cli用php -m 却看不到,为什么呢,求指教
1. 没有出现的原因是:执行时添加上php.ini的文件就可以了 $ /usr/local/php/bin/php -c /usr/local/php/etc/php.ini -m | grep ...
- Jenkins系列-Jenkins初始化配置
初始化 访问,如:127.0.0.1:8088/Jenkins 第一次要求输入密码,初始密码在文件中查看. 执行以下命令查看 $ cat ${USER_HOME}\.jenkins\secrets\i ...
- C/S结构
C/S结构 编辑 同义词 C/S架构一般指C/S结构 C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Serv ...
- MyBatis原理系列
原理分析之一:从JDBC到Mybatis 原理分析之二:框架整体设计 原理分析之三:初始化(配置文件读取和解析) 原理分析之四:一次SQL查询的源码分析
- ViewData与ViewBag
ViewData与ViewBag使用的是同一个数据源,因此数据一样,只是ViewBag 不再是字典的键值对结构,而是 dynamic 动态类型(http://www.cnblogs.com/kissd ...
- wpf拖拽
简单拖拽的实现是,实现源控件的MouseDown事件,和目标控件Drop事件.调用DragDrop.DoDragDrop()以启动拖放操作,DragDrop.DoDragDrop()函数接受三个参数: ...
- C# 面向对象——继承
继承:代码文字结合理解 class Program { static void Main(string[] args) { //Student s = new Student(); //Driver ...
- [国家集训队]最长双回文串 manacher
---题面--- 题解: 首先有一个直观的想法,如果我们可以求出对于位置i的最长后缀回文串和最长前缀回文串,那么我们枚举分界点然后合并前缀和后缀不就可以得到答案了么? 所以我们的目标就是求出这两个数列 ...