POJ
来源:互联网 发布:md204lv4编程软件下载 编辑:程序博客网 时间:2024/05/21 21:36
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 0X3f3f3f3fusing 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; else return 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 #else freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif while(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 0X3f3f3f3fusing 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 #else freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif while(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 0X3f3f3f3fusing 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 #else freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif while(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
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- javascript 复习
- git 使用规范
- java操作sqoop
- set基本用法,set接口存储一组唯一,无序的对象。数据存放无序,非常简单,主要呈现信息列表。
- Chrome 60 查看SSL证书信息, 居然有这样的操作?!
- POJ
- 设计模式之模板设计模式
- 如何获取行政区域的边界gps数据
- Java线程-10:线程的调度-合并
- Android 网络操作基础知识
- winedt 10 注册
- 微信支付应用签名修改后多久可以生效?
- Java 集合List删除元素
- 枚举理解