http://poj.org/problem?id=1273

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:87219   Accepted: 33916

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水渠中所能流过的水的最大容量.一道基础的最大流题目。

——其他练习题 POJ3436 、
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
const int N=,M=;
int head[N],tot,n,m,a,b,c;
struct node{
int next,c,to;}e[M];
struct max_flow{
int S,T,n;
int lev[N],q[N],cur[N],f;
void init(int _s,int _t)
{
tot=;S=_s,T=_t,n=T+;
FOR(i,,n) head[i]=-;
}
void add(int a,int b,int c)
{
e[tot].next=head[a];
e[tot].to=b;
e[tot].c=c;
head[a]=tot++;
}
void Add(int a,int b,int c)
{
add(a,b,c);
add(b,a,);
}
int bfs()
{
FOR(i,,n) lev[i]=;
lev[S]=,f=,q[f++]=S;
FOR(i,,f-)
{
int u=q[i];
for(int i=head[u];i!=-;i=e[i].next)
if(lev[e[i].to]==&&e[i].c>)
{
int to=e[i].to;
lev[to]=lev[u]+;
q[f++]=to;
if(to==T) return ;
}
}
return ;
}
int dfs(int u,int f)
{
if(u==T) return f;
int tag=,c;
for(int &i=cur[u];i!=-;i=e[i].next)
{
int to=e[i].to;
if(e[i].c>&&lev[to]==lev[u]+)
{
c=dfs(to,min(f-tag,e[i].c));
e[i].c-=c;
e[i^].c+=c;
tag+=c;
if(tag==f) return tag;
}
}
return tag;
}
int slove()
{
int ans=;
while(bfs())
{
FOR(i,,n) cur[i]=head[i];
ans+=dfs(S,inf);
}
return ans;
}
}flow;
int main()
{
cin.tie();
cout.tie();
while(~scanf("%d%d",&m,&n))
{
flow.init(,n);
while(m--)
{
si(a),si(b),si(c);
flow.Add(a,b,c);
}
cout<<flow.slove()<<endl;
}
return ;
}

https://blog.csdn.net/huzhengnan/article/details/7766446

最大流的一些板子

https://blog.csdn.net/wjf_wzzc/article/details/24820525

sap:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<queue>
#include<algorithm>
using namespace std; const int N=;
const int M=;
const int inf=0xfffffff; int n,m,cnt; struct Edge{
int v , cap , next;
} edge[M]; int head[N],pre[N],d[N],numd[N];//分别为链表的头指针,每个点的前驱,每个点的d值,以及标号为d[i] 的点的个数
int cur_edge[N];//从每个点出发满足d[i] = d[j] + 1的边的地址 , 插入边时的计数,源点与汇点 void addedge(int u,int v,int c){ edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = head[u];
head[u] = cnt++; edge[cnt].v = u;
edge[cnt].cap = ;
edge[cnt].next = head[v];
head[v] = cnt++;
} void bfs(int s){ //先用广度优先算出每个点的d值
memset(numd,,sizeof(numd));
for(int i=; i<=n; i++)
numd[ d[i] = n ]++;
d[s] = ;
numd[n]--;
numd[]++;
queue<int> Q;
Q.push(s); while(!Q.empty()){
int v=Q.front();
Q.pop(); int i=head[v];
while(i != -){
int u=edge[i].v; if(d[u]<n){
i=edge[i].next;
continue ;
} d[u]=d[v]+;
numd[n]--;
numd[d[u]]++;
Q.push(u);
i=edge[i].next;
}
}
} int SAP(int s,int t){
for(int i = ; i <= n; i++)
cur_edge[i] = head[i]; //当前满足d[i] = d[j] + 1的边的为第一条边
int max_flow=;
bfs(t);
int u = s ;//从源点搜一条到汇点的增广路
while(d[s]<n){//就算所有的点连成一条线源点的d值也是最多是n-1
if(u == t){//如果找到一条增广路径
int cur_flow = inf,neck;//找到那条瓶颈边
for(int from = s; from != t; from = edge[cur_edge[from]].v){
if(cur_flow > edge[cur_edge[from]].cap){
neck = from;
cur_flow = edge[cur_edge[from]].cap;
}
} for(int from = s; from != t; from = edge[cur_edge[from]].v){ //修改增广路上的边的容量
int tmp = cur_edge[from];
edge[tmp].cap -= cur_flow;
edge[tmp^].cap += cur_flow;
}
max_flow += cur_flow;//累加计算最大流
u = neck;//下一次搜索直接从瓶颈边的前一个节点搜起
} int i;
for(i = cur_edge[u]; i != -; i = edge[i].next) //从当前点开始找一条允许弧
if(edge[i].cap && d[u] == d[edge[i].v]+)//如果找到跳出循环
break; if(i!=-){//找到一条允许弧
cur_edge[u] = i;//从点u出发的允许弧的地址
pre[edge[i].v] = u;//允许弧上下一个点的前驱为u
u = edge[i].v;//u变成下一个点继续搜直到搜出一条增广路
}
else{//如果没有搜到允许弧
numd[d[u]]--; //d[u]将被修改所以numd[d[u]]减一
if(!numd[d[u]]) break; //如果没有点的d值为d[u]则不可能再搜到增广路结束搜索
cur_edge[u] = head[u]; //当前点的允许弧为第一条边
int tmp = n;
for(int j = head[u]; j != -; j = edge[j].next) //搜与u相连的点中d值最小的
if(edge[j].cap && tmp > d[edge[j].v])
tmp = d[edge[j].v]; d[u] = tmp+; //修改d[u]
numd[d[u]]++;
if(u != s)
u = pre[u];//从u的前驱搜,因为从u没有搜到允许弧
}
}
return max_flow;
}
inline void pre_init(){
cnt = ;
memset(head, -, sizeof head);
} void mapping(){
int u, v, w;
for(int i = ; i <= m; ++i){
scanf("%d %d %d", &u, &v, &w);
addedge(u, v, w);
}
} int main(){
while(~scanf("%d%d",&m,&n)){
pre_init();
mapping();
int s,t;
s = ;t = n;
int ans = SAP(s,t);
printf("%d\n",ans);
}
return ;
}

dinic:

 //dinic

 #include <algorithm>
#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int maxm = maxn*maxn;
struct node{int w; int v, next;} edge[maxm];
int pre[maxn], rec[maxn], head[maxn], block[maxn];
int dis[maxn];
int n, m, no;
int S, T;
queue<int> q;
inline void init(){
no = ;
memset(head, -, sizeof head);
}
inline void add(int u, int v, int w){
edge[no].v = v; edge[no].w = w;
edge[no].next = head[u]; head[u] = no++;
edge[no].v = u; edge[no].w = ;
edge[no].next = head[v]; head[v] = no++;
}
void reset(int S, int T){
memset(dis, 0x3f, sizeof dis);
memset(block, , sizeof block);
q.push(S); dis[S] = ;
while(!q.empty()){
int top = q.front(); q.pop();
for(int k = head[top]; k != -; k = edge[k].next)
if(dis[edge[k].v] == inf && edge[k].w)
dis[edge[k].v] = dis[top]+, q.push(edge[k].v);
}
}
int dinic(int S, int T){
int ans = , flow = inf;
int top = S;
reset(S, T); pre[S] = S;
while(dis[T] != inf){
int k, tmp;
for(k = head[top]; k != -; k = edge[k].next){
if(edge[k].w && dis[edge[k].v]==dis[top]+ &&
!block[edge[k].v]) break;
}
if(k != -){
tmp = edge[k].v;
flow = min(flow, edge[k].w);
pre[tmp] = top, rec[tmp] = k;
top = tmp;
if(top == T){
ans += flow; tmp = -;
for(; top != S; top = pre[top]){
edge[rec[top]].w -= flow;
edge[rec[top]^].w += flow;
if(!edge[rec[top]].w) tmp = top;
}
flow = inf;
if(tmp != -){
top = pre[tmp];
for(; top != S; top = pre[top])
flow = min(flow, edge[rec[top]].w);
top = pre[tmp];
}
}
}
else{
block[top] = ;
top = pre[top];
if(block[S]) reset(S, T);
}
}
return ans;
}
void mapping(){
int u, v, w;
for(int i = ; i <= m; ++i){
scanf("%d %d %d", &u, &v, &w);
add(u, v, w);
}
}
int main(){
while(~scanf("%d %d", &m, &n)){
S = , T = n;
init();
mapping();
printf("%d\n", dinic(S, T));
}
return ;
}

感谢https://www.cnblogs.com/Asumi/p/9751117.html

【最大流之Dinic算法】POJ1273 【 & 当前弧优化 & 】

https://www.cnblogs.com/DF-yimeng/p/8583698.html

//dinic

#include <algorithm>
#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int maxm = maxn*maxn;
struct node
{
int w;
int v, next;
} edge[maxm];
int pre[maxn], rec[maxn], head[maxn], block[maxn];
int dis[maxn];
int n, m, no;
int S, T;
queue<int> q;
inline void init()
{
no = ;
memset(head, -, sizeof head);
}
inline void add(int u, int v, int w)
{
edge[no].v = v;
edge[no].w = w;
edge[no].next = head[u];
head[u] = no++;
edge[no].v = u;
edge[no].w = ;
edge[no].next = head[v];
head[v] = no++;
}
void reset(int S, int T)
{
memset(dis, 0x3f, sizeof dis);
memset(block, , sizeof block);
q.push(S);
dis[S] = ;
while(!q.empty())
{
int top = q.front();
q.pop();
for(int k = head[top]; k != -; k = edge[k].next) if(dis[edge[k].v] == inf && edge[k].w) dis[edge[k].v] = dis[top]+, q.push(edge[k].v);
}
}
int dinic(int S, int T)
{
int ans = , flow = inf;
int top = S;
reset(S, T);
pre[S] = S;
while(dis[T] != inf)
{
int k, tmp;
for(k = head[top]; k != -; k = edge[k].next)
{
if(edge[k].w && dis[edge[k].v]==dis[top]+ && !block[edge[k].v]) break;
}
if(k != -)
{
tmp = edge[k].v;
flow = min(flow, edge[k].w);
pre[tmp] = top, rec[tmp] = k;
top = tmp;
if(top == T)
{
ans += flow;
tmp = -;
for(; top != S; top = pre[top])
{
edge[rec[top]].w -= flow;
edge[rec[top]^].w += flow;
if(!edge[rec[top]].w) tmp = top;
}
flow = inf;
if(tmp != -)
{
top = pre[tmp];
for(; top != S; top = pre[top]) flow = min(flow, edge[rec[top]].w);
top = pre[tmp];
}
}
}
else
{
block[top] = ;
top = pre[top];
if(block[S]) reset(S, T);
}
}
return ans;
}
void mapping()
{
int u, v, w;
for(int i = ; i <= m; ++i)
{
scanf("%d %d %d", &u, &v, &w);
add(u, v, w);
}
}
int main()
{
while(~scanf("%d %d", &m, &n))
{
S = , T = n;
init();
mapping();
printf("%d\n", dinic(S, T));
}
return ;
}

经典的最大流题POJ1273(网络流裸题)的更多相关文章

  1. 【费用流】【网络流24题】【cogs 739】运输问题

    739. [网络流24题] 运输问题 ★★ 输入文件:tran.in 输出文件:tran.out 简单对照 时间限制:1 s 内存限制:128 MB «问题描写叙述: «编程任务: 对于给定的m 个仓 ...

  2. 【费用流】【网络流24题】【P4013】 数字梯形问题

    Description 给定一个由 \(n\) 行数字组成的数字梯形如下图所示. 梯形的第一行有 \(m\) 个数字.从梯形的顶部的 \(m\) 个数字开始,在每个数字处可以沿左下或右下方向移动,形成 ...

  3. 【费用流】【网络流24题】【P1251】 餐巾计划问题

    Description 一个餐厅在相继的 \(N\) 天里,每天需用的餐巾数不尽相同.假设第 \(i\) 天需要 \(r_i\)块餐巾.餐厅可以购买新的餐巾,每块餐巾的费用为 \(p\) 分;或者把旧 ...

  4. 【费用流】【网络流24题】【P4014】 分配问题

    Description 有 \(n\) 件工作要分配给 \(n\) 个人做.第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(C_{i,j}\) .试设计一个将 \(n\) 件工作分配给 ...

  5. 【生活没有希望】poj1273网络流大水题

    你不能把数据规模改大点吗= =我优化都不加都过了 #include <cstdio> #define INF 2147483647 int n,m,ans,x,y,z,M; ],l[],f ...

  6. poj1273 网络流入门题 dinic算法解决,可作模板使用

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62078   Accepted: 2384 ...

  7. HDU-1532 网络流裸题

    HDU-1532 题意简单的来说就是从1点到n点,最大的流量是多少. 代码: #include<bits/stdc++.h> using namespace std; #define Fo ...

  8. 【最大流/二分图匹配】【网络流24题】【P3254】 圆桌问题

    Description 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,--,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,--,n) ...

  9. hdu Flow Problem (最大流 裸题)

    最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...

随机推荐

  1. java: 列出本机java环境

    java: 列出本机java环境 System.getProperties().list(System.out);

  2. note 2019.12.16

    1.无序 HTML 列表: <ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li& ...

  3. linux运维、架构之路-Lamp架构部署

    一.Lamp架构原理 二.Lamp架构软件安装 1.apache安装脚本 #!/bin/sh cd /server/tools/ yum install zlib-devel -y wget http ...

  4. css雪碧图-css精灵图

    先将图片拼接在一张图上.类似实现的效果图 图片地址为合并后的图片地址,通过background-position调整背景图的位置.效果如: HTML: <div class="logo ...

  5. mybatis insert update delete返回都是整型 0,1,增,删,改要提交事物

    mybatis insert update delete返回都是整型 0,1, 没有扔 增,删,改要提交事物

  6. CF 546 B Soldier and Badges(贪心)

    原题链接:http://codeforces.com/problemset/problem/546/B 原题描述: Soldier and Badges Colonel has n badges. H ...

  7. encodeURIComponent()加密、decodeURIComponent()解码及v-html将字符串转换为html

    1)新闻详情页后台给我的数据是加密之后的,我问了后台,是用encodeURIComponent()加密的,然后我就用对应的方法decodeURIComponent()解密: this.$store.s ...

  8. Understanding RequireJS for Effective JavaScript Module Loading

    Modular programming is used to break large applications into smaller blocks of manageable code. Modu ...

  9. CentOS7 安装Kafka

    关闭防火墙 systemctl stop firewalld.service systemctl disable firewalld.service 安装JDK yum install -y http ...

  10. day57——ajax之初体验

    转行学开发,代码100天——2018-05-12 今天是一个特别的日子——首先是母亲节,在此也祝福亲爱的妈妈健康长寿.其次今天是汶川大地震10周年,10年过去了,经历过苦难的人更加坚毅勇敢地面向未来! ...