hdoj3549 Flow Problem(裸的网络流)

本文介绍了一种经典的网络流问题,并提供了完整的解决方案。通过构建加权有向图,使用深度优先搜索寻找增广路径来求解最大流量问题。文章包含详细的算法步骤及C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flow Problem

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


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
 

Author
HyperHexagon
 

Source
 
第一道网络流题目
就是套模板


附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include<stack>
using namespace std;


typedef long long LL;
const int N=22;
const int INF=100000000;
struct edge
{
 int to,cap,res;
};
vector<edge> g[N];
int used[N];
void add_edge(int from,int to,int cap)
{
    edge tmp;
    tmp.to=to;
    tmp.cap=cap;
    tmp.res=g[to].size();
    g[from].push_back(tmp);
    tmp.to=from;
    tmp.cap=0;
    tmp.res=g[from].size()-1;
    g[to].push_back(tmp);
}
int dfs(int s,int t,int f)     // 找一条增广路
{
    if(s==t) return f;
    used[s]=1;
    for(int i=0;i<g[s].size();i++)
    {
        edge &e=g[s][i];
        int d;
        if(!used[e.to]&&e.cap>0)      // e.cap>0 要满足,不然搜下去会为负值
        {
          d= dfs(e.to,t,min(f,e.cap));
          if(d>0)
          {
              e.cap-=d;
              g[e.to][e.res].cap+=d;
              return d;
          }
        }
    }
    return 0;
}
void init()
{
    for(int i=0;i<=N;i++)
   g[i].clear();
}
LL max_flow(int s,int t)
{
    LL ans=0;
    for(;;)
    {
        memset(used,0,sizeof(used));
        LL d=dfs(s,t,INF);
        if(d==0)
            return ans;
            ans+=d;
    }
}
int main()
{
    int ca,v_sum,e_sum;
    scanf("%d",&ca);
    for(int kase=1;kase<=ca;kase++)
    {
        scanf("%d%d",&v_sum,&e_sum);
        init();
        for(int i=0;i<e_sum;i++)
        {
            int aa,bb,cc;
            scanf("%d%d%d",&aa,&bb,&cc);
            add_edge(aa,bb,cc);
        }
        printf("Case %d: ",kase);
        printf("%lld\n",max_flow(1,v_sum));
    }
    return 0;
}




































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值