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.










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
 
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枚举组合情况+最小生成树

    #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 (DFS枚举+最小生成树Prim)

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

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

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

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

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

  6. HDU 2489 Minimal Ratio Tree 最小生成树+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+DFS)

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

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

  9. Minimal Ratio Tree HDU - 2489

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

随机推荐

  1. 大数据学习——hbase的shell客户端基本使用

    1  基本shell命令 1 在hbase的 bin目录下进入命令行 ./hbase shell 2 查看有哪些表 list 3 创建一个表 create 't_user_info', {NAME = ...

  2. 第五部分 linux系统管理员 开机流程 模组管理 与loader

    第五部分   linux系统管理员  开机流程  模组管理  与loader   开机流程分析 cmos保存电脑硬件的参数 bios 基本的输入输出系统  读取硬件的软件 MBR  master bo ...

  3. Linux下dpkg的用法

    转自:http://blog.csdn.net/fireblue1990/article/details/52627952 dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理 ...

  4. (转)WaitForSingleObject函数的使用

    WaitForSingleObject 函数 DWORD WaitForSingleObject( HANDLE hObject, DWORD dwMilliseconds ); 第一个参数hObje ...

  5. 在VMWare下为CentOS设置静态IP通过NAT访问外网

    一.背景 安装好的CentOS系统默认是通过DHCP自动分配地址来共享主机的IP以达到访问外网的目的,但是因为莫名的原因无法访问外网.只好改为通过静态IP的方式访问外网. 二.操作步骤 2.1 确认开 ...

  6. 二分图最小覆盖的Konig定理及其证明,最小的覆盖证明

    [转http://www.cppblog.com/abilitytao/archive/2009/09/02/95147.html  ->  http://yejingx.ycool.com/p ...

  7. poj 3304 判断是否存在一条直线与所有线段相交

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8579   Accepted: 2608 Descript ...

  8. Nk 1430 Divisors(因子数与质因数)

    Time Limit: 5000 ms    Memory Limit: 10000 kB   Total Submit : 432 (78 users)   Accepted Submit : 10 ...

  9. Gty的妹子树(bzoj 3720)

    Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...

  10. 标准C程序设计七---22

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...