3549 Flow Problem(入门) [最大流]

Flow Problem

Time Limit: 5000 MS Memory Limit: 32768 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 30;
const int INF = 2147483647;
struct edge{
   int cap,to,rev;
   edge(int a = 0,int b = 0,int c = 0):to(a),cap(b),rev(c){}
};
vector<edge> G[MAXN];
int level[MAXN];
int iter[MAXN];

void Add(int f,int t,int c)
{
    G[f].push_back(edge{t,c,G[t].size()} );
    G[t].push_back(edge{f,0,G[f].size()-1} );
}

void bfs(int s)
{
    memset(level,-1,sizeof(level));
    level[s] = 0;
    queue<int > que;
    que.push(s);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(int i = 0;i < G[v].size();i++){
            edge &e = G[v][i];
            if(e.cap > 0 && level[e.to] == -1){
                level[e.to] = level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)
{
    if(v == t) return f;
    for(int &i = iter[v];i < G[v].size();i++){
        edge &e = G[v][i];
        if(e.cap > 0 && level[v] < level[e.to]){
            int d = dfs(e.to,t,min(f,e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }

    return 0;
}

int maxd(int s,int t)
{
    int tempf = 0;
    while(true){
        bfs(s);
        if(level[t] < 0) return tempf;
        memset(iter,0,sizeof(iter));
        int f;
        while((f = dfs(s,t,INF)) > 0 ) tempf += f;
    }
}

void init()
{
    for(int i = 0;i < MAXN;i++){
        G[i].clear();
    }
}

int main()
{
    int t;
    int Case = 1;
    while(scanf("%d",&t) != EOF){
        while(t--){
            int dsum,bsum;
            init();
            scanf("%d%d",&dsum,&bsum);
            for(int i = 0;i < bsum;i++){
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                Add(a,b,c);
            }
            printf("Case %d: %d\n",Case++,maxd(1,dsum));
        }
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值