poj1741,POJ1273 裸裸的網絡流

 2023-10-18 阅读 26 评论 0

摘要:北京好熱啊,宿舍還沒空調,都不能安安靜靜地敲代碼了~>_<~今天老師講網絡流完全沒聽啊,晚上想了好久的網絡流,感覺還是沒有完全理解,過了一道模板題。 ? Drainage Ditches ? Time Limit:?1000MS?Memory Limit:?10000KTotal Submissio

北京好熱啊,宿舍還沒空調,都不能安安靜靜地敲代碼了~>_<~今天老師講網絡流完全沒聽啊,晚上想了好久的網絡流,感覺還是沒有完全理解,過了一道模板題。

?

Drainage Ditches

?

Time Limit:?1000MS?Memory Limit:?10000K
Total Submissions:?61994?Accepted:?23794

?

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

?

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

?

Sample Output

?

50
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 int map[300][300];
 8 int pre[300];
 9 bool vis[300];
10 int n,m,s,t;
11 bool bfs()
12 {
13     int i,temp;
14     queue<int>q;
15     memset(pre,0,sizeof(pre));
16     memset(vis,0,sizeof(vis));
17     vis[s]=true;
18     q.push(s);
19     while(!q.empty())
20     {
21         temp=q.front();
22         q.pop();
23         if(temp==t) return true;
24         for(i=1;i<=n;i++)
25         {
26             if(!vis[i]&&map[temp][i])
27             {
28                 q.push(i);
29                 pre[i]=temp;
30                 vis[i]=true;
31             }
32         }
33     }
34     return false;
35 }
36 int EK()
37 {
38     int i,ans=0;
39     while(1)
40     {
41         if(!bfs()) return ans;
42         int MIN=9999999;
43         for(i=t;i!=s;i=pre[i])
44             MIN=min(MIN,map[pre[i]][i]);
45         for(i=t;i!=s;i=pre[i])
46         {
47             map[pre[i]][i]-=MIN;
48             map[i][pre[i]]+=MIN;
49         }
50         ans+=MIN;
51     }
52 }
53 int main()
54 {
55     while(~scanf("%d%d",&m,&n))
56     {
57         s=1,t=n;
58         memset(map,0,sizeof(map));
59         for(int i=0;i<m;i++)
60         {
61             int a,b,c;
62             scanf("%d%d%d",&a,&b,&c);
63             map[a][b]+=c;
64         }
65         printf("%d\n",EK());
66     }
67     return 0;
68 }
View Code

?

轉載于:https://www.cnblogs.com/zero-zz/p/4684435.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/147338.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息