您的位置:首页 > 其它

HDU 3549 Flow Problem

2016-02-03 18:32 246 查看
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网络流模板题,先来一发dinic的#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1e5 + 10;
int T, n, m, z = 0;

struct MaxFlow
{
    struct Edges
    {
        int end, flow;
        Edges(){}
        Edges(int end, int flow) :end(end), flow(flow){}
    }edge[maxn];
    int first[maxn], next[maxn], dis[maxn], tot;

    void clear(){ tot = 0; memset(first, -1, sizeof(first)); }

    void AddEdge(int s, int t, int flow)
    {
        edge[tot] = Edges(t, 0); next[tot] = first[s]; first[s] = tot++;
        edge[tot] = Edges(s, flow);    next[tot] = first[t]; first[t] = tot++;
    }

    bool bfs(int s, int t)
    {
        memset(dis, -1, sizeof(dis));
        queue<int> p;    p.push(s);    dis[s] = 0;
        while (!p.empty())
        {
            int q = p.front();    p.pop();
            for (int i = first[q]; i >= 0; i = next[i])
            {
                if (edge[i ^ 1].flow&&dis[edge[i].end] == -1)
                {
                    p.push(edge[i].end);
                    dis[edge[i].end] = dis[q] + 1;
                }
            }
        }
        return dis[t] != -1;
    }

    int dfs(int s, int t, int low)
    {
        if (s == t) return low;
        for (int i = first[s]; i >= 0; i = next[i])
        {
            if (dis[s] + 1 == dis[edge[i].end] && edge[i ^ 1].flow)
            {
                int x = dfs(edge[i].end, t, min(low, edge[i ^ 1].flow));
                if (x)
                {
                    edge[i].flow += x;    edge[i ^ 1].flow -= x;
                    return x;
                }
            }
        }
        return 0;
    }

    int dinic(int s, int t)
    {
        int maxflow = 0, inc = 0;
        while (bfs(s, t)) while (inc = dfs(s, t, 0x7FFFFFFF)) maxflow += inc;
        return maxflow;
    }
}solve;

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        int s, t, flow;
        solve.clear();
        while (m--)
        {
            scanf("%d%d%d", &s, &t, &flow);
            solve.AddEdge(s, t, flow);
        }
        printf("Case %d: %d\n", ++z, solve.dinic(1, n));
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: