POJ 1273 Drainage Ditches
2016-05-04 23:29
405 查看
Drainage Ditches
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
Sample Output
_________________________________________________________________
这好像是道网络流模板题
但为何我做得一点模板样都没有
0;}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 67387 | Accepted: 26035 |
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
_________________________________________________________________
这好像是道网络流模板题
但为何我做得一点模板样都没有
/*POJ1273 Drainage Ditches*/ //网络流模板题 #include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include<cmath> #include<queue> #include<cstring> using namespace std; const int INF=1000000; struct Ed{ int f,t; int cap,flow; Ed(int u,int v,int c,int f):f(u),t(v),cap(c),flow(f){} }; int s,n,m;//起点,结点数,终点 vector<Ed> e;//边 vector<int> G[2000];//邻接表,存储边序号 int a[600];//残量 int p[600];//保存线路用以回溯 // void clear1(int n){//初始化 for(int i=0;i<=n;i++) G[i].clear(); e.clear(); } void add_edge(int from,int to,int cap){//添加边,正反向边相邻存储 e.push_back(Ed(from,to,cap,0)); e.push_back(Ed(to,from,0,0)); int si=e.size(); G[from].push_back(si-2); G[to].push_back(si-1);//相邻两条边分别加入邻接表 return; } int fl(){ int res=0,i; for(;;){//BFS memset(a,0,sizeof(a)); queue<int>q; q.push(s); a[s]=INF; while(!q.empty()){ int x=q.front();//本轮出发点 q.pop(); for(i=0;i<G[x].size();i++){ Ed& edge=e[G[x][i]]; if(!a[edge.t] && edge.cap>edge.flow){//之前未到达,且结点有剩余流量 a[edge.t]=min(a[x],edge.cap-edge.flow); p[edge.t]=G[x][i]; q.push(edge.t); } <pre name="code" class="cpp"> if(a[m])break;//找到增广路,退出}if(!a[m])break;//无增广路,结束 for(int u=m;u!=s;u=e[p[u]].f){e[p[u]].flow+=a[m];// e[p[u]^1].flow-=a[m];}res+=a[m];}return res;}int main(){s=1;while(scanf("%d%d",&n,&m)!=EOF){clear1(n);int i,j,u,v,c;for(i=1;i<=n;i++){scanf("%d%d%d",&u,&v,&c);add_edge(u,v,c);}printf("%d\n",fl());}return
0;}
相关文章推荐
- Failed to load class "org.slf4j.impl.StaticLoggerBinder问题故障的解决
- CodeForces - 667A Pouring Rain (数学模拟)水
- LeetCode 219. Contains Duplicate II
- Leetcode - Contains Duplicate II
- Leetcode - Contains Duplicate
- TensorFlow教程04:针对机器学习初学者的MNIST实验——源码和运行结果
- ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...
- HDU 1022 Train Problem I
- (Leetcode)11. Container With Most Water
- 借助Clarifai的深度学习 电脑也能看懂视频了
- TensorFlow教程03:针对机器学习初学者的MNIST实验——回归的实现、训练和模型评估
- SCVMM-01 Create Highly Available Virtual Machines
- int i=1; main() {int i=i;}有什么错?
- Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot
- Rails--default_scope
- 子类用一个名为subitems的数组属性保存父类对象,为什么不能获取父类对象的信息呢???在main函数中subitems为空,不知道为什么。
- 启动hadoop报No Route to Host from lida1/10.30.12.87 to lida3:8485 failed on socket timeout exception:
- lightoj 1282 Leading and Trailing
- http://blog.csdn.net/jiyiqinlovexx/article/details/51173080
- 解决httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0