HDU - 3549 Flow Problem(dinic最大流)

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条边。输出从源点1到n的最大流量

简单最大流模板题。套个模板即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+10;
const int inf=0x7fffffff;
struct edge///链式前向星建图
{
    int to,val,rev;///rev用于取反边
    edge(){}
    edge(int a,int b,int c)
    {
        to=a;
        val=b;
        rev=c;
    }
};
vector<edge>mp[maxn];
void add(int from,int to,int val)///加边
{
    mp[from].push_back(edge(to,val,mp[to].size()));///正边,rev为to的最新边
    mp[to].push_back(edge(from,0,mp[from].size()-1));///反边,与正边的rev为正边的标号
}
int t,n,m;
int dep[20];///dep为dinic算法中的分层图的深度,层数为从开始到达该节点最短距离
int bfs()
{
    memset(dep,-1,sizeof dep);
    queue<int >q;
    while(!q.empty())q.pop();
    dep[1]=0;
    q.push(1);
    while(!q.empty())///广搜分层
    {
        int tmp=q.front();
        q.pop();
        if(tmp==n)return 1;///若广搜还能走到汇点说明可以继续増广
        for(int i=0;i<mp[tmp].size();i++)
        {
            int &to=mp[tmp][i].to,flow=mp[tmp][i].val;
            if(dep[to]==-1&&flow)///若该点还没有被分层,没有走到的标记,且有容量,那么走下去
            {
                dep[to]=dep[tmp]+1;
                q.push(to);
            }
        }
    }
    return 0;
}
int dfs(int s,int t,int flow)///多次増广
{
    if(s==t)return flow;///返回流量
    int pre=0;
    for(int i=0;i<mp[s].size();i++)
    {
        int &to=mp[s][i].to,val=mp[s][i].val;
        if(dep[s]+1==dep[to]&&val>0)///下一次増广的节点层数应在该点基础上+1
        {
            int tmp=min(flow-pre,val);///流量和剩余容量取最小值
            int sub=dfs(to,t,tmp);///不断通过bfs向下流,且过滤流量
            mp[s][i].val-=sub;///正向边-
            mp[to][mp[s][i].rev].val+=sub;///反向边+
            pre+=sub;
            if(pre==flow)return pre;///多次増广总流量
        }
    }
    return pre;
}///分层后每次増广多条链
int dinic()
{
    int ans=0;
    while(bfs())ans+=dfs(1,n,inf);
    return ans;
}
int main()
{
    int cas=1;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<=n;i++)mp[i].clear();
        int from,to,val;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&from,&to,&val);
            add(from,to,val);
        }
        printf("Case %d: %d\n",cas++,dinic());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值