HDU 3549 Flow Problem (最大流)

链接:click here

题意: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.

翻译:网络流量是一个众所周知的难题ACMers。给定一个图,你的任务是找出加权有向图的最大流。

输出格式:

Case 1: 1
Case 2: 2
思路:跟hdu1532 解法一样。

代码:

#include <ctype.h>  //最大流 入门
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1101;
const int inf=0x3f3f3f3f;
struct edge
{
    int to,cap,rev;
};
vector<edge > G[maxn];        //图的邻接表表示
bool used[maxn];              //访问标记
void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge)
    {
        to,cap,G[to].size()
    });
    G[to].push_back((edge)
    {
        from,0,G[from].size()-1
    });
}
int dfs(int v,int t,int f)
{
    if(v==t) return f;
    used[v]=true;
    for(int i=0; i<G[v].size(); i++)
    {
        edge &e=G[v][i];
        if(!used[e.to]&&e.cap>0)
        {
            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 max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        memset(used,0,sizeof(used));
        int f=dfs(s,t,inf);
        if(f==0) return flow;
        flow+=f;
    }
}
int main()
{
    int n,m,too,capp,revv;
    int tt,j=1;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&m,&n);
        memset(G,0,sizeof(G));
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d",&too,&capp,&revv);
            add_edge(too,capp,revv);
        }
        printf("Case %d: %d\n",j++,max_flow(1,m));
    }
    return 0;
}


测试数据:

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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值