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 ...
随机推荐
- CentOS修改DNS、IP地址、网关
一.CentOS 修改DNS 修改对应网卡的DNS的配置文件 # vi /etc/resolv.conf 修改以下内容 nameserver 8.8.8.8 #google域名服务器 nameserv ...
- python数据类型二
阅读目录 1.列表的去嵌套 2.元组 3.range 列表的增删改查 一,增: 注意 list和str是不一样的,lst可以发生改变,所以直接就在原来的对象上进行可操作 追加模式 lst = ['麻 ...
- [STL] 如何将一个vector赋给另一个vector
vector 有个函数assign, 可以帮助执行赋值操作. assign会清空你的容器. assign函数: 函数原型: void assign(const_iterator first,const ...
- 关于Python的 a, b = b, a+b
Python中有一种写法:多个值同时赋给多个变量,如:a, b = b, a+b 1. A写法 a = 0, b = 1 a, b = b, a+b print a, b #结果为:1 1 这种写法, ...
- JavaScript中:表达式和语句的区别
JavaScript中:表达式和语句的区别 Javascript语言精粹:表达式是由运算符构成,并运算产生结果的语法结构.程序是由语句构成,语句则是由“:(分号)”分隔的句子或命令.如果在表达式后面加 ...
- [洛谷P2921][USACO08DEC]在农场万圣节Trick or Treat on the Farm
题目大意:给你一张有向图,每个点最多一条出边,问从每个开始,走多少步会到一个已经过的点 题解:$tarjan$缩点,然后建反图$DP$ 卡点:无 C++ Code: #include <cstd ...
- Codeforces Round #402 (Div. 2) A B C sort D二分 (水)
A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...
- UVA315:Network(求割点)
Network 题目链接:https://vjudge.net/problem/UVA-315 Description: A Telephone Line Company (TLC) is estab ...
- Random Numbers Gym - 101466K dfs序+线段树
Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...
- [mysql]mysql弱密码字典检测
1.如何定义弱密码 和用户名一致 连续字符 连续数字 空密码 2.生成弱密码字典 3.检测脚本 4.结果