HDU2489Minimal Ratio Tree(最小生成树+状态压缩)

Minimal Ratio Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2841    Accepted Submission(s): 844

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 sub-graph of the original graph, with m nodes and whose ratio is the smallest among all the trees of m nodes in the graph.

Input

Input contains multiple test cases. The first line of each test case contains two integers n (2<=n<=15) and m (2<=m<=n), which stands for the number of nodes in the graph and the number of nodes in the minimal ratio tree. Two zeros
end the input. The next line contains n numbers which stand for the weight of each node. The following n lines contain a diagonally symmetrical n×n connectivity matrix with each element shows the weight of the edge connecting one node with another. Of course,
the diagonal will be all 0, since there is no edge connecting a node with itself.

All the weights of both nodes and edges (except for the ones on the diagonal of the matrix) are integers and in the range of [1, 100].

The figure below illustrates the first test case in sample input. Node 1 and Node 3 form the minimal ratio tree.

Output

For each test case output one line contains a sequence of the m nodes which constructs the minimal ratio tree. Nodes should be arranged in ascending order. If there are several such sequences, pick the one which has the smallest node
number; if there‘s a tie, look at the second smallest node number, etc. Please note that the nodes are numbered from 1 .

Sample Input

3 2
30 20 10
0 6 2
6 0 3
2 3 0
2 2
1 1
0 2
2 0
0 0

Sample Output

1 3
1 2
#include<stdio.h>
const int N = 20;
const int inf = 9999999;

int nodew[N],map[N][N],n,m;

int prim(int sta,int s)
{
    int ins[N]={0},node[N],ts,mint,k=m,sum;
    k--; ins[s]=1; sum=0;
    for(int i=0;i<=n;i++)
        node[i]=inf;
    node[s]=0;
    while(k)
    {
        mint=inf;
        for(int i=0;i<n;i++)
        if(!ins[i]&&((1<<i)&sta))
        {
            if(node[i]>map[s][i])
                node[i]=map[s][i];
            if(node[i]<mint)
                mint=node[i],ts=i;
        }
        if(mint!=inf)
        {
            s=ts; sum+=mint; ins[s]=1; k--;
        }
        else break;
    }
    if(k)
        return -1;
    else return sum;
}

int main()
{
    int sum_e=inf,sta;
    double sum_n;
    while(scanf("%d%d",&n,&m)>0&&n+m!=0)
    {
        for(int i=0;i<n;i++)
        scanf("%d",&nodew[i]);
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        scanf("%d",&map[i][j]);

        sum_e=inf; sum_n=1;
        for(int s=1;s<(1<<n);s++)
        {
            int k=0,id,tsum_e;
            double tsum_n=0;
            for(int i=0;i<n;i++)
                if(s&(1<<i))
                tsum_n+=nodew[i],k++,id=i;
            if(k!=m)continue;
            tsum_e=prim(s,id);
            if(tsum_e!=-1)
                if(tsum_e/tsum_n<sum_e/sum_n)
                    sum_e=tsum_e,sum_n=tsum_n,sta=s;
        }
        int i;
        for(i=0; i<n; i++)
        if((1<<i)&sta)
        {
          printf("%d",i+1); break;
        }
        for(i++ ; i<n; i++)
        if((1<<i)&sta)
        printf(" %d",i+1);
        printf("\n");
    }
}

时间: 2024-08-01 19:48:08

HDU2489Minimal Ratio Tree(最小生成树+状态压缩)的相关文章

{POJ}{3925}{Minimal Ratio Tree}{最小生成树}

题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小. 思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ里面需要memory头文件. #include <iostream> #include <vector> #include <map> #include <cmath> #include <memory> #include <algorithm> #includ

TYVJ 2054 [Nescaf&#233;29]四叶草魔杖 最小生成树 状态压缩/背包DP

$ \rightarrow $ 戳我进TYVJ原题 [Nescafé29]四叶草魔杖 题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背景 陶醉在彩虹光芒笼罩的美景之中,探险队员们不知不觉已经穿过了七色虹, 到达了目的地,面前出现了一座城堡和小溪田园,城堡前的木牌上写着"Poetic Island". "这一定就是另外两位护法的所在地了--我们快进去吧!" 探险队员们快步进入了城堡,城堡大厅的羊毛沙发上

HDU-2489 Minimal Ratio Tree(最小生成树)

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

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 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最小生成树)

题目链接: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

HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2382    Accepted Submission(s): 709 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 枚举+最小生成树

点的总数很小,直接枚举就好. #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