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

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 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

解题思路:

题意很简单。题目的数据很小,n<=15。
要找出m个点满足题目要求。
那么可以DFS枚举点,凑成m个点。
然后再在这m个点上跑一遍prim最小生成树,找到最小的路径和。然后除以所选点的和。
ps:prim很久没写,快忘了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAX=101;
const int N= 1e6+10;
int n,m;
int a[52]; // 顶点信息
int edges[52][52]; // 边信息
int node[52]; // 答案
int tmp[52];  // 所选的m个点

double ans ;

int prim(int x) // 求最小生成树
{
    int res = 0 ,mark[52],dist[52];
    memset(mark,0,sizeof(mark));
    for(int i = 0 ; i < m ; i ++)
        dist[tmp[i]] = edges[x][tmp[i]];
    mark[x] = 1;
    dist[x] = 0;
    for(int i = 0 ; i < m-1 ; i ++)
    {
        int p = x;
        for(int j = 0 ; j < m ; j ++)
        {
            if(p == x  && !mark[tmp[j]])
                p = tmp[j];
            if(!mark[tmp[j]] && dist[p] > dist[tmp[j]])
                p = tmp[j];
        }
        mark[p] = 1;
        res += dist[p];
        for(int j = 0 ; j < m ; j ++)
        {
            if(!mark[tmp[j]] && edges[p][tmp[j]] < dist[tmp[j]])
                dist[tmp[j]] = edges[p][tmp[j]];
        }
    }
    return res;
}


void dfs(int k,int num)
{
    if(num == m) // 已选出m个点就跑一遍prim算法
    {
        int sum = 0;
        for(int i = 0 ; i < m ; i ++)
            sum += a[tmp[i]];

        double res = prim(k)*1.0/sum;
        if(res-ans < -(1e-9))
        {
            ans = res;
            for(int i = 0 ; i < m ; i ++)
                node[i] = tmp[i];
        }
        return ;
    }
    if(n-1-k + num < m ) return; // 剪枝,剩下的点全选也不够m个直接返回
    for(int i = k+1 ; i < n ; i ++ ) // 还没选够m个点,则继续枚举选点。
    {
        tmp[num] = i;
        dfs(i,num+1);
    }
}

int main()
{
    while(cin>>n>>m && n+m)
    {
        ans = (1<<29)*1.0;
        for(int i = 0 ; i < n ; i ++)
        {
            cin>>a[i];
        }
        for(int i = 0 ; i < n ; i ++)
            for(int j =  0 ; j < n ;  j ++)
                cin>>edges[i][j];
        for(int i = 0 ; i < n ; i ++)
        {
            tmp[0] = i;
            dfs(i,1);
        }
        for(int i = 0 ; i < m-1 ; i ++)
            cout<<node[i]+1<<" ";
        cout<<node[m-1]+1<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值