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): 4256    Accepted Submission(s): 1346


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个点中挑选M个点,构成子图,然后求edgeweight/nodeweight最小的的解。若结果有多个,如果第一个数就不一样就输出第一个数小的那个解。

如果第一个数一样,就输出第二个数小的解,答案输出规则以此类推。

看别人博客写的。

因为节点个数并不多,所有人都是暴力求解的。从N个点挑M个构成子图。M个点确定后,nodeweight就确定,要想让边的和最小,那就要求最小生成树。


#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 17;
int N,M;
int Map1[maxn][maxn];   ///原图
int Map2[maxn][maxn];   ///子图
int chose[maxn];        ///选择的节点放在这个数组中
int weight[maxn];       ///用来选择存放N个节点的权值
int vis[maxn];          ///prim用来判断某个点是否已经加入构成最小生成树的集合
int lowcost[maxn];      ///lowcost[i]用来存放与i相连边最小的权值
int ans[maxn];          ///存放数组
double MinRatio;        ///存在最小Ratio
int prim()              ///Prim求最小生成树
{
    int start,i,j,Min,u,edgesum=0;
    start = 1;  
    /**以新图中的第一个节点为起点。这里原来我写成了start=chose[1],Wa了好久,后来才发现
    chose[1]是原来图中的节点编号,不是我现在图中第一个节点**/
    for(i = 1; i <= M; i++)
    {
        lowcost[i] = Map2[start][i];
        vis[i] = 0;
    }
    vis[start] = 1;
    for(i = 1; i < M; i++)  ///挑选M-1条边
    {
        Min = INF;
        u = 0;
        for(j = 1; j <= M; j++)
        {
            if(vis[j]==0 && lowcost[j]<Min)
            {
                Min = lowcost[j];
                u = j;
            }
        }
        vis[u] = 1;
        edgesum += Min;
        for(j = 1; j <= M; j++)
        {
            if(vis[j]==0 && Map2[u][j]<lowcost[j])
            {
                lowcost[j] = Map2[u][j];
            }
        }
    }
    return edgesum;
}
void DFS(int pos,int node) ///第pos个位置放置节点node
{
    chose[pos] = node;
    if(pos == M)           ///选够M个点
    {
        for(int i = 1; i <= M; i++)
            for(int j = 1; j <= M; j++)
            {
                Map2[i][j] = Map1[chose[i]][chose[j]]; ///构建子图
            }
        double nodeweight = 0;
        for(int i = 1; i <= M; i++)
            nodeweight += weight[chose[i]];
        double edgeweight = prim()*1.0;
        if(edgeweight/nodeweight<MinRatio)
        {
            MinRatio = edgeweight/nodeweight;
            for(int i = 1; i <= M; i++)
                ans[i] = chose[i];
        }
        return;
    }
    /**当前已经选择了pos个节点,还要挑选M-pos个节点,看剩下的节点N-node的个数
    如果不足,则直接返回**/
    if(N-node < M-pos)  ///剪枝
        return;
    for(int i = node+1; i <= N; i++)
    {
        DFS(pos+1,i);
    }
}
int main()
{
    while(~scanf("%d%d",&N,&M)) ///从N个节点中挑M个节点
    {
        if(N == 0 && M == 0)
            break;
        for(int i = 1; i <= N; i++)  ///输入N个节点的权值
            scanf("%d",&weight[i]);
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= N; j++)
                scanf("%d",&Map1[i][j]);  ///输入图
        MinRatio = INF*1.0;
        for(int i = 1; i <= N; i++) ///以第i个节点为首节点进行深搜选择M个节点
        {
            DFS(1,i);
        }
        for(int i = 1; i < M; i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[M]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值