hdu 3549 Flow Problem(简单网络流Dinic)

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

Flow Problem

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


 

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

分析:很简明的问题,就是求解最大流。

#include 
#include 
#include 
#include 
using namespace std;
const int N=20,INF=0x3f3f3f3f;
struct Dinic {
    int c[N][N], n, s, t, l[N], e[N];
    int flow(int maxf = INF) {
        int left = maxf;
        while (build()) left -= push(s, left);
        return maxf - left;
    }
    int push(int x, int f) {
        if (x == t) return f;
        int &y = e[x], sum = f;
        for (; y 0 && l[x]+1==l[y]) {
                int cnt = push(y, min(sum, c[x][y]));
                c[x][y] -= cnt;
                c[y][x] += cnt;
                sum -= cnt;
                if (!sum) return f;
            }
        return f-sum;
    }
    bool build() {
        int m = 0;
        memset(l, -1, sizeof(l));
        l[e[m++]=s] = 0;
        for (int i=0; i 0 && l[y]<0) l[e[m++]=y] = l[e[i]] + 1;
        memset(e, 0, sizeof(e));
        return l[t] >= 0;
    }
} net;
int main()
{
    //freopen("cin.txt","r",stdin);
    int ca=1,t,n,m,x,y,c;
    cin>>t;
    while(t--){
        scanf("%d%d",&n,&m);
        memset(net.c,0,sizeof(net.c));
        net.n=n;
        net.s=0;
        net.t=n-1;
        for(int i=0;i 
 

你可能感兴趣的:(图论,算法)