HDU 4700 flow Gomory–Hu tree,通过已知最大流构造图形

73 篇文章 21 订阅
56 篇文章 0 订阅

题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=4700


Gomory–Hu tree http://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree


Gomory–Hu tree的定义


Let G = ((VG, EG), c) be an undirected graph withc(u,v) being the capacity of the edge (u,v) respectively.

Denote the minimum capacity of an s- t cut by λ st for each s, tV G.
Let T = ( V T, E T) be a tree with V T = V G, denote the set of edges in an s- t path by P st for each s, tV T.

Then T is said to be a Gomory–Hu tree of G if

λ st = min e∈Pst c( S e, T e) for all s, tV G,

where

  1. Se and Te are the two connected components ofT∖{e} in the sense that (Se, Te) form as-t cut in G, and
  2. c(Se, Te) is the capacity of the cut inG.
通俗来讲就是对任意的  两个节点s,t,Gomory-Hu tree 存储的是他们各自相邻节点的最大流。

要求两个点的 最大流的话,就是求 从 s点沿着树的边走到t,其中的最小的边的权值。


此题的话,标准题解里的解释是:  如果存在这样要求的一副图的话,必然存在它所对应的 Gomory–Hu tree ,

而把Gomory–Hu tree的非相邻边设置为0,也就是可以成立的一个答案了。


标准题解:

递归构造

令 f∗ = mina̸=b Fa,b
令 A = {v0 } ∪ {v : Fv,v0 > f∗ }, B = {v : Fv,v0 = f∗ }。

若 B = ∅,失败
若 ∃(a ∈ A, b ∈ B)Fa,b > f∗ ,失败
递归构造 A, B
取 a∗ ∈ A, b∗ ∈ B,加边 (a∗ , b∗ ),容量是 f∗


代码:

#include <cassert>
#include <cstdio>
#include <cstring>
#include <climits>
#include <vector>
#include <algorithm>

#define SIZE(v) ((int)((v).size()))
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)

const int N = 100;

int n, cut[N][N], graph[N][N];

bool check(std::vector <int> vs) {
    if (SIZE(vs) <= 1) {
        return true;
    }
    int bridge = INT_MAX;
    foreach (u, vs) {
        foreach (v, vs) {
            bridge = std::min(bridge, cut[*u][*v]);
        }
    }
    int u0 = vs.front();
    std::vector <int> v0, v1;
    foreach (iter, vs) {
        int v = *iter;
        if (cut[u0][v] > bridge) {
            v0.push_back(v);
        } else {
            v1.push_back(v);
        }
    }
    if (v0.empty() || v1.empty()) {
        return false;
    }
    graph[v0.front()][v1.front()] = graph[v1.front()][v0.front()] = bridge;
    foreach (u, v0) {    //两个集合 v0跟v1中的最大流是 bridge,如果给出的条件中 flow(a,b) > bridge,则与条件矛盾,构造不出该图形
        foreach (v, v1) {
            if (cut[*u][*v] != bridge) {
                return false;
            }
        }
    }
    return check(v0) && check(v1);
}

int main() {
//	freopen("1005.in","r",stdin);
//	freopen("10050.out","w",stdout);
    freopen("temp.in","r",stdin);
    while (scanf("%d", &n) == 1) {
    //	printf("%d\n",n);
        assert(1 <= n && n <= N);
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                assert(scanf("%d", &cut[i][j]) == 1);
             //   if(j!=n-1)printf("%d ",cut[i][j]);
             //   else printf("%d",cut[i][j]);
                if (i == j) {
                    assert(cut[i][j] == -1);
                    cut[i][j] = INT_MAX;
                } else {
                    assert(0 <= cut[i][j] && cut[i][j] <= 1000000000);
                }
            }
        //    puts("");
        }
        for (int i = 0; i < n; ++ i) {
            for (int j = 0; j < n; ++ j) {
                assert(cut[i][j] == cut[j][i]);
            }
        }
        std::vector <int> vertices;
        for (int i = 0; i < n; ++ i) {
            vertices.push_back(i);
        }
        memset(graph, 0, sizeof(graph));
        for (int i = 0; i < n; ++ i) {
            graph[i][i] = -1;
        }
        if (check(vertices)) {
            puts("YES");
            for (int i = 0; i < n; ++ i) {
                for (int j = 0; j < n; ++ j) {
                    //printf("%d%c", i == j ? -1 : 0, j == n - 1 ? '\n' : ' ');
                    printf("%d%c", graph[i][j], j == n - 1 ? '\n' : ' ');
                }
            }
        } else {
            puts("NO");
        }
    }
    //puts(check(vertices) ? "YES" : "NO");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值