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模板填充数据,并以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 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];