HDU 4859 最大流最小割经典题目

本文详细介绍了如何使用最大流最小割算法解决HDU4859问题,通过C++实现了一个具体的例子。文章包含了完整的源代码,并解释了核心的数据结构和算法流程。

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

//
//  main.cpp
//  HDU 4859 最大流最小割
//
//  Created by 郑喆君 on 8/10/14.
//  Copyright (c) 2014 itcast. All rights reserved.
//

#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;
const int maxn = 50*50;

struct Edge{
    int from, to, cap, flow;
    Edge(int _from, int _to, int _cap, int _flow):from(_from),to(_to),cap(_cap),flow(_flow){}
};
vector<Edge> es;
vector<int> g[maxn];
int s,t;
int p[maxn],d[maxn],gap[maxn],cur[maxn];
void addedge (int from, int to, int cap){
    es.push_back(Edge(from, to, cap, 0));
    es.push_back(Edge(to, from, 0,0));
    int num = es.size();
    g[from].push_back(num-2);
    g[to].push_back(num-1);
}
void BFS (){
    queue<int> q;
    memset(d, -1, sizeof(d));
    d[t] = 0;
    q.push(t);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = 0; i < g[u].size(); i++){
            Edge& e = es[g[u][i]];
            if(g[u][i]&1 && d[e.to]==0){
                d[e.to] = d[u]+1;
                q.push(e.to);
            }
        }
    }
}
int augment (){
    int x = t;
    int a = int_max;
    while(x!=s){
        Edge& e = es[p[x]];
        a = (a < e.cap-e.flow ? a : e.cap-e.flow);
        x = es[p[x]].from;
    }
    x = t;
    while(x!=s){
        es[p[x]].flow += a;
        es[p[x]^1].flow -= a;
        x = es[p[x]].from;
    }
    return a;
}
int sap (){
    int flow = 0;
    BFS();
    memset(gap, 0, sizeof(gap));
    for(int i = 1; i <= t; i++) if(d[i]!=-1) gap[d[i]]++;
    int x = s;
    memset(cur, 0, sizeof(cur));
    while(d[s] < t+1){
        if(x==t){
            flow += augment();
            x = s;
        }
        int ok = 0;
        for(int i = 0; i < g[x].size(); i++){
            Edge& e = es[g[x][i]];
            if(e.cap > e.flow && d[x]==d[e.to]+1){
                ok = 1;
                p[e.to] = g[x][i];
                cur[x] = i;
                x = e.to;
                break;
            }
        }
        if(!ok){
            int mm = t;
            for(int i = 0; i < g[x].size(); i++){
                Edge& e = es[g[x][i]];
                if(e.cap > e.flow) mm = (mm > d[e.to] ? d[e.to] : mm);
            }
            if(--gap[d[x]] == 0) break;
            gap[d[x]=mm+1]++;
            cur[x] = 0;
            if(x!=s) x = es[p[x]].from;
        }
    }
    return flow;
}
int n,m,mymap[50][50];
int main(int argc, const char * argv[])
{
    int T;
    int cnt = 1;
    scanf("%d", &T);
    while (T--) {
        scanf("%d %d", &n, &m);
        memset(mymap, 0, sizeof(mymap));
        for(int i = 1; i <= m+2; i++) {mymap[1][i] = 1; mymap[n+2][i] = 1;}
        for(int i = 1; i <= n+2; i++) {mymap[i][1] = 1; mymap[i][m+2] = 1;}
        for(int i = 2; i <= n+1; i++){
            char cs[50];
            scanf("%s", cs);
            for(int j = 2; j <= m+1; j++){
                if(cs[j-2]=='D') mymap[i][j] = 1;
                if(cs[j-2]=='.') mymap[i][j] = 2;
            }
        }
        es.clear();
        for(int i = 0; i < maxn; i++) g[i].clear();
        s = 0;
        t = (n+2)*(m+2)+1;
        int dir[4][2] = {-1,0,0,1,1,0,0,-1};
        for(int i = 1; i <= n+2; i++)
            for(int j = 1; j <= m+2; j++){
                if((i+j)%2==0){
                    if(mymap[i][j]==2) addedge(s, (i-1)*(m+2)+j, int_max);
                    if(mymap[i][j]==1) addedge((i-1)*(m+2)+j, t, int_max);
                }else{
                    if(mymap[i][j]==1) addedge(s, (i-1)*(m+2)+j, int_max);
                    if(mymap[i][j]==2) addedge((i-1)*(m+2)+j, t, int_max);
                }
                for(int u = 0; u < 4; u++){
                    int ii = i + dir[u][0];
                    int jj = j + dir[u][1];
                    if(ii>=1 && ii <=n+2 && jj>=1 && jj<= m+2) addedge((i-1)*(m+2)+j, (ii-1)*(m+2)+jj, 1);
                }
            }
        int tot = (m+1)*(n+2) + (n+1)*(m+2);
        printf("Case %d: %d\n", cnt++, tot-sap());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值