HDU 1532 Drainage Ditches (最大網路流)

來源:互聯網
上載者:User
Drainage Ditches

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


Problem DescriptionEvery
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.  

 

InputThe
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. 

 

OutputFor each case, output a single integer, the maximum rate at which water may emptied from the pond.  

 

Sample Input
5 41 2 401 4 202 4 202 3 303 4 10

 

Sample Output
50

解題:

       最大網路流的模板題,從網上找了一個不錯的模板,而有待理解。

#include <iostream><br />using namespace std;<br />#define MAXN 205<br />#define INF 2110000000<br />#define MIN(x,y) (x<y?x:y)<br />int map[MAXN][MAXN];<br />int max_flow(int num,int map[][MAXN],int source,int sink)//參數含義:結點數量 網路 源點 匯點<br />{<br />int my_queue[MAXN],queue_first,queue_end;//數組做隊列 實現BFS搜尋路徑<br />int pre[MAXN],min_flow[MAXN];//記錄結點的父節點 當前路徑中最小的一段的值,也即限制值<br />int flow[MAXN][MAXN];//記錄當前網路中的流<br />int ans=0;//最終結果<br />memset(flow,0,sizeof(flow));<br />while(1)//一直迴圈,直到不存在增廣路徑<br />{<br />queue_first=0;//初始化隊列<br />queue_end=0;<br />my_queue[queue_end++]=source;<br />memset(pre,-1,sizeof(pre));<br />min_flow[source]=INF;<br />pre[source]=-2;//源點的父節點需特殊標示<br />while(queue_first<queue_end)//BFS尋找增廣路徑<br />{<br />int temp=my_queue[queue_first++];//出隊列<br />for(int i=0;i<num;i++)//由結點temp往外擴充<br />{<br />if(pre[i]==-1&&flow[temp][i]<map[temp][i])//當結點i還未被探索到,並且還有可用流量<br />{<br />my_queue[queue_end++]=i;//排入佇列<br />pre[i]=temp;//標示父節點<br />min_flow[i]=MIN(min_flow[temp],(map[temp][i]-flow[temp][i]));//求得min_flow<br />}<br />}<br />if(pre[sink]!=-1)//sink的父節點不為初始值,說明BFS已經找到了一條路徑<br />{<br />int k=sink;<br />while(pre[k]>=0)<br />{<br />flow[pre[k]][k]+=min_flow[sink];//將新的流量加入flow<br />flow[k][pre[k]]=-flow[pre[k]][k];<br />k=pre[k];<br />}<br />break;<br />}<br />}<br />if(pre[sink]==-1) return ans;//不存在增廣路徑,返回<br />else ans+=min_flow[sink];<br />}<br />}<br />int main()<br />{<br />int m,n;<br />while(cin>>n>>m)<br />{<br />int a,b,cost;<br />memset(map,0,sizeof(map));<br />for(int i=0;i<n;i++)<br />{<br />cin>>a>>b>>cost;<br />map[a-1][b-1]+=cost;<br />}<br />cout<<max_flow(m,map,0,m-1)<<endl;<br />}<br />return 0;<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.