hdu - 2489 - Minimal Ratio Tree(枚举 + MST)

题意:给出一个图 n x n (2<=n<=15)的图,每个点,每条边都有权值,求其中的 m (2<=m<=n)个点,使得这m个点生成的树的边点权比例最小。

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

——>>数量小,于是,可以枚举取 m 个点的所有情况,对每种情况最一次MST,更新最小值。。

时间复杂度:O(n ^ n * log(n) * 2 ^ n)

#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>

using std::priority_queue;

const int MAXN = 15;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;

struct EDGE
{
    int from;
    int to;
    int w;

    EDGE() {}
    EDGE(int from, int to, int w) : from(from), to(to), w(w) {}

    bool operator < (const EDGE& e) const
    {
        return w > e.w;
    }
};

int n, m;
int sume, sumn;
int node[MAXN];
int G[MAXN][MAXN];
int toUse[MAXN], ucnt;
int fa[MAXN];

void Init()
{
    sume = 0;
    sumn = 0;
}

int Find(int x)
{
    return x == fa[x] ? x : (fa[x] = Find(fa[x]));
}

void Union(int x, int y)
{
    int xroot = Find(x);
    int yroot = Find(y);

    if (xroot != yroot)
    {
        fa[yroot] = xroot;
        sume += G[x][y];
    }
}

void Read()
{
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", node + i);
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            scanf("%d", &G[i][j]);
        }
    }
}

int Ones(int x)
{
    int ret = 0;

    while (x)
    {
        if (x & 1)
        {
            ++ret;
        }
        x >>= 1;
    }

    return ret;
}

void GetUseNode(int S)
{
    ucnt = 0;
    for (int j = 0; (1 << j) <= S; ++j)
    {
        if ((1 << j) & S)
        {
            toUse[ucnt++] = j;
            sumn += node[j];
        }
    }
}

void InitUnion()
{
    for (int j = 0; j < n; ++j)
    {
        fa[j] = j;
    }
}

void Kruscal()
{
    priority_queue<EDGE> pq;
    for (int j = 0; j < ucnt; ++j)
    {
        for (int k = j + 1; k < ucnt; ++k)
        {
            pq.push(EDGE(toUse[j], toUse[k], G[toUse[j]][toUse[k]]));
        }
    }
    while (!pq.empty())
    {
        EDGE e = pq.top();
        pq.pop();
        Union(e.from, e.to);
    }
}

void Output(int ret)
{
    bool fst = true;
    for (int i = 0; (1 << i) <= ret; ++i)
    {
        if ((1 << i) & ret)
        {
            if (fst)
            {
                fst = !fst;
            }
            else
            {
                putchar(' ');
            }
            printf("%d", i + 1);
        }
    }
    puts("");
}

int Dcmp(double x)
{
    if (fabs(x) < EPS) return 0;
    return x > 0 ? 1 : -1;
}

void Solve()
{
    int ret = 0;
    double Min = INF;

    for (int i = 3; i < (1 << n); ++i)
    {
        if (Ones(i) != m) continue;
        Init();
        GetUseNode(i);
        InitUnion();
        Kruscal();

        double r = (double)sume / sumn;
        if (Dcmp(r - Min) < 0)
        {
            Min = r;
            ret = i;
        }
    }

    Output(ret);
}

int main()
{
    while (scanf("%d%d", &n, &m) == 2)
    {
        if (!n && !m) break;
        Read();
        Solve();
    }
    return 0;
}
时间: 2025-01-01 20:51:37

hdu - 2489 - Minimal Ratio Tree(枚举 + MST)的相关文章

hdu 2489 Minimal Ratio Tree 枚举+最小生成树

点的总数很小,直接枚举就好. #include <stdio.h> #include <string.h> #define N 20 #define inf 1000000 int mk[N],n,k,ans[N]; double low[N],val[N]; double map[N][N],MIN; double prim() { int i,j; double sum=0; double tot=0; for(i=1;i<=n;i++) low[i]=inf; int

hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~

题目: 链接:点击打开链接 题意: 输入n个点,要求选m个点满足连接m个点的m-1条边权值和sum与点的权值和ans使得sum/ans最小,并输出所选的m个点,如果有多种情况就选第一个点最小的,如果第一个点也相同就选第二个点最小的........ 思路: 求一个图中的一颗子树,使得Sum(edge weight)/Sum(point weight)最小~ 数据量小,暴力枚举~~~~~dfs暴力枚举C(M,N)种情况. 枚举出这M个点之后,Sum(point weight)固定,进行prim或者K

HDU 2489 Minimal Ratio Tree(数据结构-最小生成树)

Minimal Ratio Tree Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a

hdu 2489 Minimal Ratio Tree DFS枚举点+最小生成树 属于中等偏上题 ,Double比较大小的时候注意精度问题

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2835    Accepted Submission(s): 841 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a tree, which is a su

HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges

HDU 2489 Minimal Ratio Tree

Minimal Ratio Tree Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 248964-bit integer IO format: %I64d      Java class name: Maina For a tree, which nodes and edges are all weighted, the ratio of it is calcul

HDU 2489 Minimal Ratio Tree(prim+DFS)

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3345    Accepted Submission(s): 1019 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it i

hdu 2489 Minimal Ratio Tree (DFS枚举+MST)

参考链接:http://blog.csdn.net/xingyeyongheng/article/details/9373271 http://www.cnblogs.com/chenxiwenruo/p/3294668.html 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include