Minimal Ratio Tree

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 <iostream>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std; int a[],f[],p[];
int mp[][];
bool vis[];
int i,j,n,m;
double ans;
void prim() //最小生成树
{
bool vis[];
int dis[];
int sumnode,sumedge=,k;
memset(vis,,sizeof(vis));
vis[]=;
sumnode=p[a[]];
for(int i=;i<=m;i++) dis[i]=mp[a[]][a[i]];
for(int i=;i<m;i++)
{
int minn=INT_MAX;
for(int j=;j<=m;j++)
{
if (!vis[j] && dis[j]<minn)
{
minn=dis[j];
k=j;
}
}
vis[k]=;
sumedge+=minn;
sumnode+=p[a[k]];
for(int j=;j<=m;j++)
if (!vis[j] && dis[j]>mp[a[k]][a[j]])
dis[j]=mp[a[k]][a[j]];
}
double w=sumedge*1.0/sumnode;
if (w<ans) //把最优解存放在f数组中
{
ans=w;
for(int i=;i<=m;i++)
f[i]=a[i];
}
return;
}
void dfs(int k,int num)//dfs暴力枚举m个节点是哪几个存在a数组中
{
if (num==m)
{
prim();
return;
}
if (k>n) return; if (!vis[k])
{
vis[k]=;
a[num+]=k;
dfs(k+,num+);
vis[k]=;
}
dfs(k+,num);
return;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if (n== && m==) break;
for(i=;i<=n;i++) scanf("%d",&p[i]);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&mp[i][j]);
memset(vis,,sizeof(vis));
ans=INT_MAX*1.0;
dfs(,);
for(i=;i<m;i++) printf("%d ",f[i]);
printf("%d\n",f[m]);
}
return ;
}

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)的更多相关文章

  1. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...

  3. HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)

    Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...

  4. HDU 2489 Minimal Ratio Tree 最小生成树+DFS

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. HDU 2489 Minimal Ratio Tree(prim+DFS)

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. hdu 2489 Minimal Ratio Tree

    http://acm.hdu.edu.cn/showproblem.php?pid=2489 这道题就是n个点中选择m个点形成一个生成树使得生成树的ratio最小.暴力枚举+最小生成树. #inclu ...

  7. hdu2489 Minimal Ratio Tree dfs枚举组合情况+最小生成树

    #include <stdio.h> #include <set> #include <string.h> #include <algorithm> u ...

  8. Minimal Ratio Tree HDU - 2489

    Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...

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

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. css3 box-shadow 阴影的用法

    text-shadow是给文本添加阴影效果,box-shadow是给元素块添加周边阴影效果.随着html5和CSS3的普及,这一特殊效果使用越来越普遍. 基本语法是{box-shadow:[inset ...

  2. Threading

    new System.Threading.Thread(new System.Threading.ThreadStart(ReadState)).Start();

  3. 去掉matlab图片空白边缘

    在图形文件figure的菜单上点击file->export setup size选项中,对"expand axes to fill figure"选项打勾,如下图:

  4. PHP ServerPush (推送) 技术

    用来代替ajax的请求 转自:http://blog.163.com/bailin_li/blog/static/17449017920124811524364/ 需求: 我想做个会员站内通知的功能. ...

  5. web上传大文件的配置

    1.项目本身的webconfig  在<system.web>字段下 <httpRuntime targetFramework="4.5" requestLeng ...

  6. 在block函数中规避错误信息 "capturing self strongly in this block is likely to lead to a retain cycle”

    以形如 _fontValueChangedBlock = ^(){ [self.fontSmallButton addTarget:self action:@selector(btnFontSmall ...

  7. Android上的远程调试

    来源: http://www.seejs.com/archives/296 目录 远程调试概述 使用 Chrome 的 ADB 扩展进行远程调试 1. 安装 ADB 扩展 2. 启用你的移动设备上的 ...

  8. 基于QTcpSocket和QTcpServer的Tcp通讯以及QDataStream序列化数据

    最近要在QT下开发Tcp通讯,发送序列化数据以便于接收. 这里涉及到几个问题: 1.QTcpSocket.QTcpServer的通讯 2.QDataStream序列化数据 多的不说,直接上干货!!! ...

  9. Git客户端SourceTree回滚到远程仓库和切换分支

    使用SourceTree将远程仓库回滚到某一次提交 原理:在本地需要回滚的commit上创建一个分支,将该分支合并到远程仓库. 步骤 1.在需要回滚的commit上右键创建分支 创建分支 2.输入新的 ...

  10. ssl证书验证

    当我们在访问https网站时,浏览器就会自动下载该网站的SSL证书,并对证书的安全性进行检查. 其他概念不说了,有效期之类的验证也不说了.只说数字证书的真实性和可信性验证. 1.CA下发给网站的证书是 ...