HDU ------ (3549) Flow Problem (maximum flow (Water Body ))

Source: Internet
Author: User
Flow Problem

Time Limit: 5000/5000 MS (Java/others) memory limit: 65535/32768 K (Java/Others)
Total submission (s): 8203 accepted submission (s): 3817


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 input23 21 2 12 3 13 31 2 12 3 11 3 1

 

Sample outputcase 1: 1 case 2: 2

 

Authorhyperhexagon

 

Source hyperhexagon's summer gift (original tasks) code:
 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<queue> 5 using namespace std; 6 const int inf=0x3f3f3f3f; 7 int mat[30][30]; 8 int dist[31]; 9 int n,m;10 int min(int a,int b)11 {12 return a<b?a:b;13 }14 bool bfs(int st,int to){15     memset(dist,-1,sizeof(dist));16     queue<int>q;17     q.push(st);18     dist[st]=0;19     int t;20     while(!q.empty()){21        t=q.front();22        q.pop();23     for(int i=1;i<=n;i++){24         if(dist[i]<0&&mat[t][i]>0){25             dist[i]=dist[t]+1;26             if(i==to)return 1;27             q.push(i);28         }29     }30     }31   return 0;32 }33 int dfs(int st,int to,int flow)34 {35     int tem;36     if(st==to||flow==0) return flow;37     for(int i=1;i<=n;i++){38       if((dist[i]==dist[st]+1)&&mat[st][i]>0&&(tem=dfs(i,to,min(mat[st][i],flow))))39       {40           mat[st][i]-=tem;41           mat[i][st]+=tem;42           return tem;43       }44     }45   return 0;46 }47 int Dinic(int st,int en)48 {49     int ans=0;50     while(bfs(st,en))51         ans+=dfs(st,en,inf);52    return ans;53 }54 int main(){55   int cas,i,a,b,c;56   scanf("%d",&cas);57   for(i=1;i<=cas;i++){58       scanf("%d%d",&n,&m);59       memset(mat,0,sizeof(mat));60      while(m--){61        scanf("%d%d%d",&a,&b,&c);62        mat[a][b]+=c;63      }64    printf("Case %d: %d\n",i,Dinic(1,n));65   }66   return 0;67 }
View code

 

HDU ------ (3549) Flow Problem (maximum flow (Water Body ))

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.