HDU 3549(網路流)

來源:互聯網
上載者:User
Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 123    Accepted Submission(s): 78

Problem DescriptionNetwork flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. 

 

InputThe first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000) 

 

OutputFor each test cases, you should output the maximum flow from source 1 to sink N. 

 

Sample Input
23 21 2 12 3 13 31 2 12 3 11 3 1
 

 

Sample Output
Case 1: 1Case 2: 2

 

#include <iostream><br />using namespace std;</p><p>const int maxn=150000;<br />const int maxm=2000000;<br />const int inf=1<<30;<br />struct edge{int from, to, val, next;}e[maxm];;<br />int v[maxn], que[maxn], dis[maxn],len;<br />void init()<br />{<br /> len=0;<br /> memset(v, -1, sizeof(v));<br />}<br />void insert(int from, int to, int va)<br />{<br /> e[len].from = from, e[len].to = to; e[len].val = va;<br /> e[len].next = v[from];v[from] = len++;<br /> e[len].from = to, e[len].to = from; e[len].val = 0;<br /> e[len].next = v[to];v[to] = len++;<br />}<br />int Dinic(int n, int s, int t)<br />{<br /> int ans = 0;<br /> while(true)<br /> {<br /> int head, tail, id,i;<br /> head = tail = 0; que[tail++] = s;<br /> memset(dis, -1, sizeof(dis)); dis[s] = 0;<br /> while(head < tail)<br /> {<br /> id = v[que[head++]];<br /> while(id != -1)<br /> {<br /> if (e[id].val > 0 && dis[e[id].to] == -1)<br /> {<br /> dis[e[id].to] = dis[e[id].from] + 1;<br /> que[tail++] = e[id].to;<br /> if (e[id].to == t)<br /> {<br /> head = tail;<br /> break;<br /> }<br /> }<br /> id = e[id].next;<br /> }<br /> }<br /> if (dis[t] == -1) break;<br /> id = s, tail = 0;<br /> while(true)<br /> {<br /> if (id == t) // 找到一條增廣路<br /> {<br /> int flow =inf,fir;<br /> for(i = 0; i < tail; i++)<br /> if (e[que[i]].val < flow)<br /> {<br /> fir = i;<br /> flow = e[que[i]].val;<br /> }<br /> for(i = 0; i < tail; i++)<br /> e[que[i]].val -= flow, e[que[i] ^ 1].val += flow;<br /> ans += flow; tail = fir; id = e[que[fir]].from;<br /> }<br /> id = v[id];<br /> while(id != -1)<br /> {<br /> if (e[id].val > 0 && dis[e[id].from] + 1 == dis[e[id].to])<br /> break;<br /> id = e[id].next;<br /> }<br /> if (id != -1)<br /> {<br /> que[tail++] = id;<br /> id = e[id].to;<br /> }<br /> else<br /> {<br /> if (tail == 0) break;<br /> dis[e[que[tail - 1]].to] = -1;<br /> id = e[que[--tail]].from;<br /> }<br /> }<br /> }<br /> return ans;<br />}<br />int main()<br />{<br /> int t,n,m,a,b,c,cas;<br /> while (scanf("%d",&t)!=EOF)<br /> {<br /> cas = 0;<br /> while (t--)<br /> {<br /> init();<br /> scanf("%d%d",&n,&m);<br /> while (m--)<br /> {<br /> scanf("%d%d%d",&a,&b,&c);<br /> insert(a-1,b-1,c);<br /> }<br /> printf("Case %d: %d/n",++cas,Dinic(n,0,n-1));<br /> }<br /> }<br /> return 0;<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.