Drainage Ditches POJ - 1273
2017-04-28 01:33
369 查看
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
题意:..网络流标准模型… 1是源点 n是汇点 求个最大流2333
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
题意:..网络流标准模型… 1是源点 n是汇点 求个最大流2333
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<vector> #include<set> #include<queue> #include<map> #include<sstream> #include<stack> #include<functional> #include<cctype> using namespace std; typedef long long ll; //thanks to pyf ... //thanks to lmd ... #define INF 0x3f3f3f3f #define CLR(a,b) memset(a,b,sizeof(a)) const int N = 5005; struct Edge { int from,to,cap,flow; Edge(int u,int v,int c,int f) : from(u),to(v),cap(c),flow(f){} }; struct Dinic { int s,t,m,n; bool vis ; int d ; int cur ; vector<int>G ; vector<Edge>edges; void add_edge(int u,int v,int cap) { edges.push_back(Edge(u,v,cap,0)); edges.push_back(Edge(v,u,0,0)); int m = edges.size(); G[u].push_back(m-2); G[v].push_back(m-1); } bool bfs() { memset(vis,0,sizeof(vis)); queue<int>q; q.push(s); d[s] = 0; vis[s] = 1; while(!q.empty()) { int x = q.front(); q.pop(); for(int i=0;i<G[x].size();i++) { Edge &e = edges[G[x][i]]; if(!vis[e.to]&&e.cap>e.flow) { vis[e.to] = 1; q.push(e.to); d[e.to] = d[x] +1; } } } return vis[t]; } int dfs(int x,int a) { if(x==t||a==0) return a; int flow = 0,f; for(int &i = cur[x];i<G[x].size();i++) { Edge &e = edges[G[x][i]]; if(d[e.to]==d[x]+1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0) { e.flow += f; edges[G[x][i]^1].flow -= f; flow+=f; a-=f; if(!a) break; } } return flow; } int Maxflow(int s,int t) { this -> s = s; this -> t = t; int flow = 0; while(bfs()) { CLR(cur,0); flow+=dfs(s,INF); } return flow; } }; int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { Dinic ans; for(int i=0;i<n;i++) { int u,v,d; scanf("%d%d%d",&u,&v,&d); ans.add_edge(u,v,d); } int s = 1; int t = m; printf("%d\n",ans.Maxflow(s,t)); } }
相关文章推荐
- POJ 1273 Drainage Ditches(网络流-最大流)
- POJ 1273 Drainage Ditches dinic 最大流
- 网络流poj(1273)Drainage Ditches
- POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall 最大流简单题
- POJ 1273 && HDU 1532:Drainage Ditches
- POJ 1273(HDU 1532) Drainage Ditches|HDU 3549 Flow Problem|很典型&&裸的网络流
- POJ 1273:Drainage Ditches 网络流模板题
- [POJ 1273] Drainage Ditches & 最大流Dinic模板
- POJ 1273:Drainage Ditches 网络流模板题
- Drainage Ditches poj 1273 最大流
- POJ1273——Drainage Ditches//模板还没搞清楚待定
- POJ 1273 Drainage Ditches
- POJ 1273 (基础最大流) Drainage Ditches
- POJ 1273 Drainage Ditches
- 【POJ 1273】Drainage Ditches&Dinic详解
- POJ_1273_Drainage-Ditchs_图论_最大流
- POJ - 1273 Drainage Ditche
- POJ 1273 Drainage Dithches 最大流(EK,模板题)
- poj Drainage Ditches
- poj 1273 Drainage Ditches