题目链接:poj.org/problem?id=1273

题目:

题意:求最大流。

思路:测板子题,分别用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)的更多相关文章

  1. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  2. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  3. POJ1273 Drainage Ditches (网络流)

                                                             Drainage Ditches Time Limit: 1000MS   Memor ...

  4. POJ-1273 Drainage Ditches 最大流Dinic

    Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...

  5. POJ 1273 Drainage Ditches(网络流,最大流)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  6. POJ_1273 Drainage Ditches 【网络流】

    一.题面 Drainage Ditches 二.分析 网络流的裸题. 1 Edmonds-Karp算法 求解网络流其实就是一个不断找增广路,然后每次找到一条增广路后更新残余网络的一个过程. EK算法主 ...

  7. HDU 1532 Drainage Ditches (网络流)

    A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1532 Drainage Ditches (最大网络流)

    Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) To ...

  9. 网络流dinic ek模板 poj1273

    这里只是用来存放模板,几乎没有讲解,要看讲解网上应该很多吧…… ek bfs不停寻找增广路到找不到为止,找到终点时用pre回溯,O(VE^2) #include<cstdio> #incl ...

随机推荐

  1. linux核心版本号的说明

    日志不会很长,因为每天都在学习,我认为的重点,我自己做的记录,我很高兴能分享给大家: Linux的核心版本编号有点类似如下癿样子: 2.6.18-92.el5 主版本.次版本.释出版本-修改版本 因为 ...

  2. ASP.NET MVC 多语言解决方案

    1:打开VS,新建ASP.NET MVC4项目 2:创建一个放本地化资源的文件夹并命名为"Language",右键选择添加新项,选择资源文件并命名为"Com" ...

  3. Python 时间推进器-->在当前时间的基础上推前n天 | CST时间转化标准日期格式

    由于公司任务紧迫,好久没有在园子里写自己的心得了,今天偷个闲发表点简单的代码块,在开源的时代贡献微薄力量.话不多说,直接上代码块: ]) m = ]) d = ]) the_date = dateti ...

  4. Visual C++中对运行时库的支持

    原文地址:http://blog.csdn.net/wqvbjhc/article/details/6612099 一.什么是C运行时库 1)C运行时库就是 C run-time library,是 ...

  5. 线程同步(使用了synchronized)和线程通讯(使用了wait,notify)

    线程同步 什么是线程同步? 当使用多个线程来访问同一个数据时,非常容易出现线程安全问题(比如多个线程都在操作同一数据导致数据不一致),所以我们用同步机制来解决这些问题. 实现同步机制有两个方法:1.同 ...

  6. 【题解】CF#983 E-NN country

    首先,我们从 u -> v 有一个明显的贪心,即能向上跳的时候尽量向深度最浅的节点跳.这个我们可以用树上倍增来维护.我们可以认为 u 贪心向上跳后不超过 lca 能跳到 u' 的位置, v 跳到 ...

  7. [NOI2017]蔬菜 贪心

    题面: [NOI2017]蔬菜 题解: 首先每天蔬菜会变质这点并不好处理,我们考虑让时间倒流,从后向前处理,这样的话就相当于每天都会得到一定量的蔬菜. 这样做有什么好处呢? 我们可以发现一个性质:如果 ...

  8. 后端日期类属性date 不接受string类型日期,都是没找到解决的方法,所有前端传回的string字符串都一一转化为java定义的类型

    1.比如日期 我们可以是yyyy-MM-dd 亦可以是 yyyy-MM-dd HH:mm:ss 方法1在java代码中需要的字段上加上注解 写上日期类型,不过这样很麻烦,每个人写了日期类型的接收前端的 ...

  9. 如何写出高性能DOM?

    为什么要写高性能DOM? 一个网站,在页面上承载最多内容的就是DOM,而且无论是我们通过加载JS.加载图片,他们也是通过写HTML标签来实现的.而我们性能优化要做的无非就是几大块: 站点的网络消耗 D ...

  10. 让ie8、ie9支持媒体查询

    <!-- 让IE8/9支持媒体查询,从而兼容栅格 --> <!--[if lt IE 9]> <script src="https://cdn.staticfi ...