POJ - 1273(最大流模板)

2024-01-03 12:38
文章标签 模板 最大 poj 1273

本文主要是介绍POJ - 1273(最大流模板),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Drainage Ditches

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

题意:……..

思路:模板

EK

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=300+10;
const double eps=1e-6;int n,m;
int G[MAX][MAX];
int pre[MAX],vis[MAX];
int flow[MAX];void updata(int node,int flow){while(pre[node]!=-1){G[pre[node]][node]-=flow;G[node][pre[node]]+=flow;node=pre[node];}
}int bfs(int s,int t){memset(vis,0,sizeof(vis));memset(pre,-1,sizeof(pre));vis[s]=1;int minn=inf;queue<int>q;q.push(s);flow[s]=inf;while(q.size()){int u=q.front();q.pop();if(u==t)    break;for(int i=1;i<=n;i++){if(!vis[i]&&G[u][i]){q.push(i);flow[i]=min(G[u][i],flow[u]);pre[i]=u;vis[i]=1;}}}if(pre[t]==-1)return 0;elsereturn flow[t];
}int edmonds_karp(int s,int t){int temp=0;int ans=0;do{temp=bfs(s,t);updata(t,temp);ans+=temp;}while(temp);return ans;
}int main(){#ifdef ONLINE_JUDGE#elsefreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);#endifwhile(cin>>m>>n){memset(flow,0,sizeof(flow));memset(G,0,sizeof(G));for(int i=1;i<=m;i++){int u,v,c;cin>>u>>v>>c;if(u==v)    continue;G[u][v]+=c;}cout<<edmonds_karp(1,n)<<endl;}return 0;
}

Ford-Fulkerson

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=500+10;
const double eps=1e-6;int n,m;
struct EDGE{int v,c,rev;
};
vector<EDGE>G[MAX];
bool vis[MAX];void init(){for(int i=1;i<=n;i++)G[i].clear();
} void addedge(int u,int v,int c){G[u].push_back((EDGE){v,c,G[v].size()});G[v].push_back((EDGE){u,0,G[u].size()-1});
}int dfs(int v,int t,int f){if(v==t)    return f;vis[v]=1;for(int i=0;i<G[v].size();i++){EDGE &e=G[v][i];if(!vis[e.v]&&e.c>0){int d=dfs(e.v,t,min(f,e.c));if(d>0){e.c-=d;G[e.v][e.rev].c+=d;return d;}}}return 0;
}int max_flow(int s,int t){int flow=0;while(1){memset(vis,0,sizeof(vis));int f=dfs(s,t,inf);if(!f)  return flow;flow+=f;}
}int main(){#ifdef ONLINE_JUDGE#elsefreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);#endifwhile(cin>>m>>n){init();int u,v,c;for(int i=1;i<=m;i++){cin>>u>>v>>c;addedge(u,v,c);}cout<<max_flow(1,n)<<endl;}return 0;
}

Dinic

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=500+10;
const double eps=1e-6;int n,m;
struct EDGE{int v,c,rev;
};
vector<EDGE>G[MAX];
int level[MAX];//定点到原点的距离标号
int iter[MAX];//当前弧void init(){for(int i=1;i<=n;i++)G[i].clear();
}void addedge(int u,int v,int cap){G[u].push_back((EDGE){v,cap,G[v].size()});G[v].push_back((EDGE){u,0,G[u].size()-1});
}//BFS计算从源点出发的距离标号
void bfs(int s){memset(level,-1,sizeof(level));queue<int>q;level[s]=0;q.push(s);while(q.size()){int u=q.front();q.pop();for(int i=0;i<G[u].size();i++){EDGE &e=G[u][i];if(e.c>0&&level[e.v]<0){level[e.v]=level[u]+1;q.push(e.v);}}}
}//dfs寻找增广路
int dfs(int v,int t,int f){if(v==t)    return f;for(int &i=iter[v];i<G[v].size();i++){EDGE &e=G[v][i];if(e.c>0&&level[v]<level[e.v]){int d=dfs(e.v,t,min(f,e.c));if(d>0){e.c-=d;G[e.v][e.rev].c+=d;return d;}}}return 0;
}int max_flow(int s,int t){int flow=0;while(1){bfs(s);if(level[t]<0)  return flow;memset(iter,0,sizeof(iter));int f;while((f=dfs(s,t,inf))>0)flow+=f;}
}int main(){#ifdef ONLINE_JUDGE#elsefreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);#endifwhile(cin>>m>>n){init();int u,v,c;for(int i=1;i<=m;i++){scanf("%d%d%d",&u,&v,&c);addedge(u,v,c);}cout<<max_flow(1,n)<<endl;}return 0;
}

这篇关于POJ - 1273(最大流模板)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565719

相关文章

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

基于Java实现模板填充Word

《基于Java实现模板填充Word》这篇文章主要为大家详细介绍了如何用Java实现按产品经理提供的Word模板填充数据,并以word或pdf形式导出,有需要的小伙伴可以参考一下... Java实现按模板填充wor编程d本文讲解的需求是:我们需要把数据库中的某些数据按照 产品经理提供的 word模板,把数据

如何提高Redis服务器的最大打开文件数限制

《如何提高Redis服务器的最大打开文件数限制》文章讨论了如何提高Redis服务器的最大打开文件数限制,以支持高并发服务,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录如何提高Redis服务器的最大打开文件数限制问题诊断解决步骤1. 修改系统级别的限制2. 为Redis进程特别设置限制

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�