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

时间:2022-03-11 18:07:26

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 calculated according to the following equation.





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




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.


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

 
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
 
Source

⊙﹏⊙‖∣在推断两浮点数大小时应该这样比較:a-b<-(1e-8);我由于不知道这个WA了6次。

题意:求一个稍微变形的“最小生成树”,其值为边权和除以点权和。

题解:用深搜在n个点里选出m个点。再求这m个点的“最小生成树”就可以。

#include <stdio.h>
#include <string.h>
#include <limits.h>
#define maxn 16 int map[maxn][maxn], node[maxn];
int n, m, store[maxn], vis[maxn];
double ans;
bool visted[maxn]; //hash to vis array double prim()
{
int i, j, u, count = 0, tmp, vnv = 0, vne = 0;
for(i = 1; i <= m; ++i) vnv += node[vis[i]];
memset(visted, 0, sizeof(visted));
visted[1] = 1;
while(count < m - 1){
for(i = 1, tmp = INT_MAX; i <= m; ++i){
if(!visted[i]) continue;
for(j = 1; j <= m; ++j){
if(!visted[j] && map[vis[i]][vis[j]] < tmp){
tmp = map[vis[i]][vis[j]]; u = j;
}
}
}
if(tmp != INT_MAX){
visted[u] = 1;
vne += tmp; ++count;
}
}
return vne * 1.0 / vnv;
} void DFS(int k, int id)
{
if(id > m){
double tmp = prim();
if(tmp - ans < -(1e-8)){
ans = tmp; memcpy(store, vis, sizeof(vis));
}
return;
}
for(int i = k; i <= n; ++i){
vis[id] = i;
DFS(i + 1, id + 1);
}
} int main()
{
int i, j;
while(scanf("%d%d", &n, &m), n || m){
for(i = 1; i <= n; ++i) scanf("%d", &node[i]);
for(i = 1; i <= n; ++i)
for(j = 1; j <= n; ++j)
scanf("%d", &map[i][j]);
ans = INT_MAX;
DFS(1, 1);
for(i = 1; i <= m; ++i)
if(i != m) printf("%d ", store[i]);
else printf("%d\n", store[i]);
}
return 0;
}

HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】的更多相关文章

  1. hdu2489 Minimal Ratio Tree dfs枚举组合情况&plus;最小生成树

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

  2. hdu2489 Minimal Ratio Tree

    hdu2489 Minimal Ratio Tree 题意:一个 至多  n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...

  3. HDU 2489 Minimal Ratio Tree &lpar;DFS枚举&plus;最小生成树Prim&rpar;

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

  4. HDU 2489 Minimal Ratio Tree &lpar;dfs&plus;Prim最小生成树&rpar;

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

  5. HDU 2489 Minimal Ratio Tree&lpar;dfs枚举&plus;最小生成树&rpar;

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

  6. HDU 2489 Minimal Ratio Tree 最小生成树&plus;DFS

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

  7. HDU 2489 Minimal Ratio Tree(prim&plus;DFS)

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

  8. HDU 2489 Minimal Ratio Tree(暴力&plus;最小生成树)(2008 Asia Regional Beijing)

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

  9. Minimal Ratio Tree HDU - 2489

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

随机推荐

  1. &lbrack;WinAPI&rsqb; API 13 &lbrack;遍历指定目录 打印文件和其他属性&rsqb;

    Windows API中,有一组专门的函数和结构,用于遍历目录,它们是FindFirstFile函数.FindNextFile函数和WIN32_FIND_DATA结构.使用FindFirstFile和 ...

  2. codeforces 86D &colon; Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  3. Delphi TcxTreeList 节点添加图片

    需要给TcxTreelist的列添加图片,操作如下 1.设置列, 设置Properties为ImageComboBox , 2. 设置Properties -> Items 添加内容 对应的增加 ...

  4. mysql System Tablespace

    System Tablespace 数据文件配置: mysql> show variables like '%innodb_data_file_path%'; +---------------- ...

  5. IOS开发&colon;xcode5版本引发的问题

    下面这段代码是用于处理ios7头部透明问题的 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 if ( IOS7_OR_LATER ) { self.e ...

  6. git 常用使用及问题记录

    1.打开bash,进入工程根目录(引用whaon的话:是和.classpath和.project同级的目录).PS:我的系统是win7,在bash切换到E的命令是 cd /e: 2.运行 git in ...

  7. IT项目角色标准定义

    角色 角色标准定义 项目主管 负责协助项目经理分配资源,确定优先级,协调公司和项目组之间的沟通.保证项目团队一直处于良好的状态中.同时监督项目经理的工作方法,以确保项目以及工件符合公司的发展方向以及用 ...

  8. Java的家庭记账本程序(E)

    日期:2019.2.9 博客期:032 星期二 今天是把程序的相关Bug补一补,嗯`: 1.添加了跳转说明 生成了一个对于成员的权限声明内容,用户再登陆界面点击Go按钮后,切换至说明页面,再次点击Go ...

  9. Linux 文件系统概览

    本文导航 -定义07% -文件系统的基本功能12% -目录结构26% -Linux 统一目录结构50% -文件系统类型74% -挂载81% -结论90% -下个月92%   本文旨在高屋建瓴地来讨论 ...

  10. &lbrack;IR&rsqb; Bigtable&colon; A Distributed Storage System for Semi-Structured Data

    良心博文: http://blog.csdn.net/opennaive/article/details/7532589 这里只是基础简述 众人说: 链接:http://blog.csdn.net/o ...