hdu 2498 Minimal Ratio Tree

先用DFS求出全组合,然后每个组合求最小生成树。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n, m, summ;
int u[10000][20];
int t[20], ff[20], nodecost[20], father[20];
int edgecost[20][20];
struct abc{ int start, end, cost; }node[20 * 20];
int find(int x)
{
    if (x != father[x]) father[x] = find(father[x]);
    return father[x];
}
void dfs(int tot, int i)
{
    int j;
    if (tot == m){ for (j = 0; j < m; j++) u[summ][j] = t[j]; summ++; return; }
    if (i > n) return;
    t[tot] = i; dfs(tot + 1, i + 1); dfs(tot, i + 1);
}
bool cmp(const abc&a, const abc&b){ return a.cost < b.cost; }
int main()
{
    int i, j, ii;
    while (~scanf("%d%d", &n, &m))
    {
        if (n == 0 && m == 0) break;
        summ = 0; dfs(0, 1);//DFS生成全组合
        for (i = 1; i <= n; i++) scanf("%d", &nodecost[i]);
        for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) scanf("%d", &edgecost[i][j]);
        int sx = 0;
        for (i = 1; i <= n; i++)
        {
            for (j = i + 1; j <= n; j++)
            {
                node[sx].start = i;
                node[sx].end = j;
                node[sx].cost = edgecost[i][j];
                sx++;
            }
        }
        sort(node, node + sx, cmp);
        double minratio = 999999999;
        int edge_weight, node_weight, anss;
        for (ii = 0; ii < summ; ii++)
        {
            memset(ff, 0, sizeof(ff));
            edge_weight = 0, node_weight = 0;
            for (i = 0; i <= n; i++) father[i] = i;
            for (i = 0; i < m; i++) ff[u[ii][i]] = 1, node_weight = node_weight + nodecost[u[ii][i]];
            for (i = 0; i < sx; i++)
            {
                if (ff[node[i].start] && ff[node[i].end])
                {
                    int x = find(node[i].start);
                    int y = find(node[i].end);
                    if (x != y)
                    {
                        father[x] = y;
                        edge_weight = edge_weight + node[i].cost;
                    }
                }
            }
            if (1.0*edge_weight / node_weight < minratio) minratio = 1.0*edge_weight / node_weight, anss = ii;
        }
        for (i = 0; i < m; i++)
        {
            if (i < m - 1) printf("%d ", u[anss][i]);
            else if (i == m - 1) printf("%d\n", u[anss][i]);
        }
    }
    return 0;
}
时间: 2024-12-29 12:47:17

hdu 2498 Minimal Ratio Tree的相关文章

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 枚举+最小生成树

点的总数很小,直接枚举就好. #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(数据结构-最小生成树)

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+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 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(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(枚举 + 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> #in