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. C++中的临时变量

    临时变量有两个特征: 1.invisiable,在程序代码中没有显式出现 2 没有名字 non - named. 出现临时变量/对象的场合1.函数的返回值2. 参数传递 值传递 by - value ...

  2. MySQL主从复制报错一致性问题解决

    当MySQL主从复制中因为不一致报错的情况,我们第一时间想到的就是使用pt-table-checksum来进行检查主从一致并进行修复,但是使用此工具的前提是主从复制线程都为on状态, 所以这种情况下可 ...

  3. JS 调用存储过程传递参数

    引用 #region 程序集 Newtonsoft.Json.dll, v4.5.0.0 // E:\Newtonsoft.Json.dll #endregion public DataTable R ...

  4. 也来“玩”Metro UI之磁贴(一)

    Win8出来已有一段时间了,个人是比较喜欢Metro UI的.一直以来想用Metro UI来做个自己的博客,只是都没有空闲~~今天心血来潮,突然想自己弄一个磁贴玩玩,动手……然后就有了本篇. Win8 ...

  5. iRule Event Order - HTTPSv7

    v

  6. 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分

    题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...

  7. HDU 5833 Zhu and 772002 ——线性基

    [题目分析] 这题貌似在UVA上做过,高精度高斯消元. 练习赛T2,然后突然脑洞出来一个用Bitset的方法. 发现代码只需要30多行就A掉了 Bitset大法好 [代码] #include < ...

  8. [TyvjP1519] 博彩游戏(AC自动机 + DP)

    传送门 和bzoj1030一个德性 #include <queue> #include <cstdio> #include <cstring> #define N ...

  9. python和scrapy的安装【转:https://my.oschina.net/xtfjt1988/blog/364577】

    抓取网站的代码实现很多,如果考虑到抓取下载大量内容scrapy框架无疑是一个很好的工具.Scrapy = Search+Pyton.下面简单列出安装过程.PS:一定要按照Python的版本下载,要不然 ...

  10. LA 3135 优先队列

    题目大意:有若干命令,它有两个属性Q_Num,Period(周期).按时间循序模拟前k个命令并输出他们的Q_Num,若同时发生输出Q_Num最小的值. #include<iostream> ...