当前位置 : 主页 > 手机开发 > harmonyos >

hdu 3549 最大网络流 Flow Problem

来源:互联网 收集:自由互联 发布时间:2023-08-26
Problem Description Network 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. Input The first line of input contains an integer T, denoting the numbe


Problem Description


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




Input


The 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)




Output


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




Sample Input


2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1




Sample Output


Case 1: 1 Case 2: 2




Author


HyperHexagon




Source


HyperHexagon's Summer Gift (Original tasks) 

#include<stdio.h> 
 
 #include<string.h> 
 
 #include<queue> 
 
 #include<iostream> 
 
 #include<stdlib.h> 
 
 using namespace std; 
 
 #define maxn 550 
 
 #define INF 99999999 
 


 int level[maxn]; 
 
 int N,M; 
 
 struct ele 
 
 { 
 
     int c; 
 
     int f; 
 
 }G[maxn][maxn]; 
 


 bool bfs() 
 
 { 
 
     int v,u; 
 
     memset(level,0,sizeof(level)); 
 
     level[1]=1; 
 
     queue<int>Q; 
 
     Q.push(1); 
 
     while(!Q.empty()) 
 
     { 
 
         u=Q.front(); 
 
         Q.pop(); 
 
         for(v=1;v<=N;v++) 
 
         { 
 
             if(!level[v]&&G[u][v].c>G[u][v].f) 
 
             { 
 
                 level[v]=level[u]+1; 
 
                 Q.push(v); 
 
             } 
 
         } 
 
     } 
 
     return level[N]!=0; 
 
 } 
 


 int dfs(int u,int cp) 
 
 { 
 
     int v,t; 
 
     int temp=cp; 
 
     if(u==N) 
 
         return cp; 
 
     for(v=1;v<=N&&temp;v++) 
 
      { 
 
          if(level[v]==level[u]+1) 
 
             if(G[u][v].c>G[u][v].f) 
 
             { 
 
                 t=dfs(v,min(temp,G[u][v].c-G[u][v].f)); 
 
                 temp-=t; 
 
                 G[u][v].f+=t; 
 
                 G[v][u].f-=t; 
 
             } 
 
      } 
 
      return cp-temp; 
 
 } 
 


 void dinic() 
 
 { 
 
     int sum=0,t; 
 
     while(bfs()) 
 
     { 
 
         while(t=dfs(1,INF)) 
 
         { 
 
             sum+=t; 
 
         } 
 
     } 
 
     printf("%d\n",sum); 
 
 } 
 


 int main() 
 
 { 
 
     int T; 
 
     int i; 
 
     int x,y,z; 
 
     scanf("%d",&T); 
 
     for(i=1;i<=T;i++) 
 
     { 
 
         memset(G,0,sizeof(G)); 
 
         scanf("%d%d",&N,&M); 
 
         while(M--) 
 
         { 
 
             scanf("%d%d%d",&x,&y,&z); 
 
             G[x][y].c+=z; 
 
         } 
 
         printf("Case %d: ",i); 
 
         dinic(); 
 
     } 
 


 }

网友评论