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 ...
随机推荐
- 《剑指offer》---顺时针打印矩阵
本文算法使用python3实现 1. 问题1 1.1 题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 ...
- iOS开发JOSNModel<optional>,<convertondemand>,<index>
指定定义的key的类型 <optional>表示字段可选,例如 //链接字段是可选的,转换的时候允许link未空 @property (nonatomic,strong) NSString ...
- 转 Redis集群技术及Codis实践
转 Redis集群技术及Codis实践 转自 :http://blog.51cto.com/navyaijm/1637688 codis开源地址:https://github.com/CodisLa ...
- 载入其他同名源文件导致vs编译错误
今天下午工程编译的时候总是通不过,提示1,某个类没有某个成员,可是我去该类的头文件下查看,确实包括了这个成员啊.2,没有某个类,可是我明明定义了的. 检查了好久才发现 原来是,我打开了其他工程下的某一 ...
- 第52天:offset家族、scroll家族和client家族的区别
一.offset家族 1.offsetWidth offsetHeight offsetLeft offsetTop offsetParent共同组成了offset家族,用来获取元素尺寸. offse ...
- Hessian矩阵【转】
http://blog.sina.com.cn/s/blog_7e1ecaf30100wgfw.html 在数学中,海塞矩阵是一个自变量为向量的实值函数的二阶偏导数组成的方块矩阵,一元函数就是二阶导, ...
- BZOJ 1149 风铃(树形DP)
题目描述的实际是一颗二叉树,对于每个结点,要么满叉,要么无叉. 对于一种无解的简单情况,我们搜一遍树找到最浅的叶子结点1和最深的叶子结点2,如果dep[1]<dep[2]-1,则显然无解. 所以 ...
- 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- BZOJ4200 & 洛谷2304 & UOJ132:[NOI2015]小园丁与老司机——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4200 https://www.luogu.org/problemnew/show/P2304 ht ...
- SQL Server 2008设置主键为自增
环境:SQL Server 2008 问题:设置主键,将主键设为自增. 解决:点击table->选中表->design->选中需要设置主键的字段,单击右键"设置主键&quo ...