HDU 3549 Flow problem

Flow Problem

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

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
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  1532 3572 3416 3081 3491 

 

第一次做网络流的题目,求一个最大流

用 Dinic 算法 ,有向图 , 反向弧的容量设为 0 。

每次用 bfs 构建层次图, 若果 vis[ t ] == 1 的话证明图上还是有增广路 。

然后用 递归dfs 寻找增广路 。

dfs 过程除了当前结点  x 以外 , 还需要传入 一个表示“ 目前为止所有弧的最小残量 ” 的 a , 

当 x 为汇点 或者 a = 0的时候终止dfs 过程 ,否则多路增广 。

有一个重要的优化 就是保存每一个结点 x 正在考虑的 弧 cur[x] , 以避免重复计算 。( dfs 更新增广路之前把eh 复制给 cur 就ok 了)

 

复制代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
const int INF = 1e9 ;
const int N = 20;
const int M = 2010;

bool vis[N];
int eh[N],et[M],nxt[M],ef[M],ec[M],etot;
int s , t , n , m;
int d[N],cur[N];
void init(){
    memset( eh, -1 , sizeof eh) ;
    etot =0 ;
}
void addedge( int u , int v , int c ,int f){
    et[etot] = v ; nxt[etot] = eh[u]; ef[etot] = f; ec[etot] = c ; eh[u] = etot++;
    et[etot] = u ; nxt[etot] = eh[v]; ef[etot] = f; ec[etot] = 0 ; eh[v] = etot++;
}

bool bfs ()
{
    memset( vis , 0 , sizeof vis);
    queue< int >que;
    que.push(s) ;
    d[s] = 0;
    vis[s] =1 ;
    while( !que.empty() ) {
        int u = que.front(); que.pop();
        for( int i = eh[ u ] ; ~i ; i = nxt[i] ){
            int v = et[i] , c = ec[i] , f = ef[i];
            if( !vis[v] && c > f){
                vis[v] = 1 ;
                d[v] = d[u] + 1;
                que.push(v);
            }
        }
    }
    return vis[t];
}

int dfs (int x ,int a)
{
    if ( x == t || a == 0 ){
        return a ;
    }
    int flow = 0 , F;
    for( int &i = cur[x] ; ~i ; i = nxt[i] ){
        int v = et[i] , c = ec[i] , &f = ef[i];
        if( d[x] + 1 == d[v] && ( F = dfs (v, min( a, c - f))) > 0) {
            f += F;
            ef[ i ^ 1 ] -= F;
            flow += F;
            a -= F;
            if( a == 0 )break;
        }
    }
    return flow;
}

int MF(){
    int flow = 0 ;
    while( bfs() ){
        memcpy( cur , eh , sizeof eh );
        flow += dfs(s , INF);
    }
    return flow;
}

void run()
{
    int u,v,c;

    scanf("%d%d",&n,&m);

    init();

    while(m--){
        scanf("%d%d%d",&u, &v, &c);
        addedge( u ,v , c , 0 );
    }
    s = 1 , t = n;
    printf("%d\n",MF());
}

int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    int _ , cas = 1 ;
    scanf("%d",&_);
    while(_--){
        printf("Case %d: ",cas++);
        run();
    }
    return 0;
}
复制代码

 

posted @   hl_mark  阅读(147)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
点击右上角即可分享
微信分享提示