前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu-----(1532)Drainage Ditches(最大流问题)

hdu-----(1532)Drainage Ditches(最大流问题)

作者头像
Gxjun
发布2018-03-26 14:33:31
6230
发布2018-03-26 14:33:31
举报
文章被收录于专栏:ml

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9580    Accepted Submission(s): 4541

Problem 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

Source

USACO 93

题意:  有关输水网络,给出每一段水管的最大输水流量,问整个系统出水的最大流量是多少......

看到这道题,完全惊呆了,一道原生态的网络流问题,而且问题还这么的直白。╮(╯▽╰)╭,但是呵呵,杭电有展现了他机智的一面,说的数据和给的数据不一致,说好的200,然后要到了500,然后果断的献上了好几个wa。

看了一下discuss里的僵尸版的提示,又去修改一下代码....O(∩_∩)O~呵~呵,感觉不会再爱了,居然还有重边,我真的是....,优秀改了一下矩阵的构图,又大方的献了几个wa....

最后终于AC...当然对于这样的一个水体(带坑),也还是要高兴高兴....

首先献上一个EK算法的代码:(不知道什么时候学的,貌似是算法竞赛里刘奴佳介绍的)..时间复杂度有点小高...但是数据很弱居然oms过了,我又.......感受到了这个世界的恶意啊╮(╯▽╰)╭

  0ms

代码语言:javascript
复制
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 const int inf=0x3f3f3f3f;
 8 const int maxn=501;
 9 int map[maxn][maxn];
10 int dist[maxn];
11 int n,m;
12 int  bfs(int st,int en){
13     int t;
14     queue<int>q;
15     memset(dist,-1,sizeof(int)*(m+1));
16     q.push(st);
17     dist[st]=0;
18     while(!q.empty()){
19         t=q.front();
20         q.pop();
21       for(int i=1;i<=m;i++){
22         if(map[t][i]>0&&dist[i]<0){
23             dist[i]=dist[t]+1;
24             q.push(i);
25         }
26       }
27     }
28    if(dist[en]>0) return 1;
29     return 0;
30 }
31 int dfs(int st,int en,int flow){
32    int  tem=0;
33    if(st==en||flow==0)return flow;
34    for(int i=1;i<=m;i++)
35    {
36         if(dist[i]==dist[st]+1&&map[st][i]>0&&(tem=dfs(i,en,min(map[st][i],flow))))
37      {
38           map[st][i]-=tem;
39           map[i][st]+=tem;
40           return tem;
41      }
42    }
43    return 0;
44 }
45 void Dinic(int st,int en)
46 {
47     int ans=0;
48     while(bfs(st,en))
49         ans+=dfs(st,en,inf);
50   printf("%d\n",ans);
51 }
52 int main()
53 {
54   int i,a,b,c;
55   while(scanf("%d%d",&n,&m)!=EOF){
56       memset(map,0,sizeof(map));
57       for(i=1;i<=n;i++){
58       scanf("%d%d%d",&a,&b,&c);
59        map[a][b]+=c;
60       }
61       Dinic(1,m);
62   }
63  return 0;
64 }

 优化优化,用一下邻接表做...

代码:内存立马减少到了 276k

代码:

代码语言:javascript
复制
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 #define ma 502
 5 #define inf 0x3f3f3f3f
 6 using namespace std;
 7 int head[ma];
 8 struct node
 9 {
10   int to;
11   int w;
12   int next;
13 };
14 node mat[ma];
15 int dist[ma];
16 int pos,n,m;
17 int min(int a,int b){
18     return a>b?b:a;
19 }
20 void add(int a,int b,int flow){
21         mat[pos].to=b;
22         mat[pos].w=flow;
23         mat[pos].next=head[a];
24         head[a]=pos++;
25 }
26 
27 bool bfs(int st,int to){
28     memset(dist,-1,sizeof(int)*(n+1));
29     queue<int> q;
30     q.push(st);
31     dist[st]=0;
32     int t;
33     while(!q.empty()){
34         t=q.front();
35         q.pop();
36       for(int i=head[t];~i;i=mat[i].next){
37          if(dist[mat[i].to]<0&&mat[i].w>0){
38              dist[mat[i].to]=dist[t]+1;
39              if(mat[i].to==to) return 1;
40              q.push(mat[i].to);
41          }
42       }
43     }
44    return 0;
45 }
46 
47 int dfs(int st,int to,int flow){
48 
49     int tem=0;
50     if(st==to||flow==0) return flow;
51     for(int i=head[st];~i;i=mat[i].next){
52       if(mat[i].w>0&&dist[mat[i].to]==dist[st]+1&&(tem=dfs(mat[i].to,to,min(flow,mat[i].w))))
53         {
54             mat[i].w-=tem;
55             mat[i^1].w+=tem;
56             return tem;
57         }
58     }
59    return 0;
60 }
61 int Dinic(int st,int to){
62     int ans=0;
63     while(bfs(st,to))
64        ans+=dfs(st,to,inf);
65     return ans;
66 }
67 
68 int main()
69 {
70     int a,b,c;
71     while(scanf("%d%d",&m,&n)!=EOF)
72     {
73         memset(head,-1,sizeof(int)*(n+1));
74         pos=0;
75         while(m--){
76          scanf("%d%d%d",&a,&b,&c);
77          add(a,b,c);  //单向边
78          add(b,a,0);
79         }
80         printf("%d\n",Dinic(1,n));
81     }
82     return 0;
83 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Drainage Ditches
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档