HDU-3549-Flow Problem(网络流+Dinic)

http://acm.hdu.edu.cn/showproblem.php?pid=3549

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

题目大意:t组样例,每组样例n,m代表n行m列,接下来是这个图,i,j的数字表示

输出s - t的最大流,板子题,随便找个板子,复制一下就过了,就不多说了。

ac:

//一页 27行就很舒服 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

int mp[20][20];
int dis[20];
int n,m;

bool bfs(int s)
{
    clean(dis,-1);
    dis[s]=0;
    queue<int> que;
    que.push(s);
    while(que.size())
    {
        int u=que.front();
        que.pop();
        for(int i=1;i<=n;++i)
        {
            if(dis[i]<0&&mp[u][i]>0)
            {
                dis[i]=dis[u]+1;
                que.push(i);
            }
        }
    }
    //cout<<dis[n]<<endl;
    if(dis[n]>0)
        return 1;
    return 0;
}

int Find(int x,int low)
{
    int a=0;
    if(x==n)
        return low;
    for(int i=1;i<=n;++i)
    {
        if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=Find(i,min(low,mp[x][i]))))
        {
            mp[x][i]=mp[x][i]-a;
            mp[i][x]=mp[i][x]+a;
            return a;
        }
    }
    return 0;
}

int main()
{
    int t,cas=1;
    cin>>t;
    while(t--)
    {
        clean(mp,0);
        cin>>n>>m;
        for(int i=0;i<m;++i)
        {
            int u,v,w;
            cin>>u>>v>>w;
            mp[u][v]=mp[u][v]+w;
        }
        int ans=0,tmp;
        while(bfs(1))
        {
            while(tmp=Find(1,INF))
                ans=ans+tmp;
//            for(int i=1;i<=n;++i)
//            {
//                for(int j=1;j<=n;++j)
//                    cout<<mp[i][j]<<" ";
//                cout<<endl;
//            }
        }
        cout<<"Case "<<cas++<<": "<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81807589
今日推荐