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. Configure Red Hat Enterprise Linux shared disk cluster for SQL Server

    下面一步一步介绍一下如何在Red Hat Enterprise Linux系统上为SQL Server配置共享磁盘集群(Shared Disk Cluster)及其相关使用(仅供测试学习之用,基础篇) ...

  2. Ubuntu Software Center has closed unexpectly解决方案

    打开软件中心Ubuntu Software Center的时候 出现crash report :The application Ubuntu Software Center has closed un ...

  3. Linux 查看端口占用并杀掉进程

    1. 查看端口号占用情况: netstat -apn|grep 11305 tcp        0      0 10.65.42.27:80              172.22.142.20: ...

  4. UVA 11297 Census ——二维线段树

    [题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> # ...

  5. 算法复习——计算几何基础(zoj1081)

    题目: Statement of the Problem Several drawing applications allow us to draw polygons and almost all o ...

  6. 【单调队列+尺取】HDU 3530 Subsequence

    acm.hdu.edu.cn/showproblem.php?pid=3530 [题意] 给定一个长度为n的序列,问这个序列满足最大值和最小值的差在[m,k]的范围内的最长子区间是多长? [思路] 对 ...

  7. java面试题之Thread的run()和start()方法有什么区别

    run()方法: 是在主线程中执行方法,和调用普通方法一样:(按顺序执行,同步执行) start()方法: 是创建了新的线程,在新的线程中执行:(异步执行) public class App { pu ...

  8. angular实时显示checkbox被选中的元素

    /** * Created by zh on 20/05/15. */ // Code goes here var iApp = angular.module("App", []) ...

  9. CentOS 7 使用iptables 开放端口

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service system ...

  10. 三类(创结行),23种设计模式,速记理解法!PHP

    一,创建型设计模式 1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳 ...