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 .

题目大意:给n个点,一个完全图,要求你选出m个点和m-1条边组成一棵树,其中sum(边权)/sum(点权)最小,并且字典序最小,输出这m个点。

思路:大水题,n个选m个,$C_{n}^{m}$最大也不到1W,最小生成树算法也才$O(n^2)$,果断暴力。暴力枚举选和不选,然后用最小生成树求sum(边权),逐个比较即可。

PS:太久没写最小生成树结果混入了最短路的东西结果WA了>_<

代码(15MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int MAXN = ;
const int INF = 0x3fff3fff; int mat[MAXN][MAXN];
int weight[MAXN];
int n, m;
bool use[MAXN], vis[MAXN];
int dis[MAXN]; int prim(int st) {
memset(vis, , sizeof(vis));
vis[st] = true;
for(int i = ; i <= n; ++i) dis[i] = mat[st][i];
int ret = ;
for(int cnt = ; cnt < m; ++cnt) {
int u, min_dis = INF;
for(int i = ; i <= n; ++i)
if(use[i] && !vis[i] && dis[i] < min_dis) u = i, min_dis = dis[i];
ret += min_dis;
vis[u] = true;
for(int i = ; i <= n; ++i)
if(use[i] && !vis[i] && dis[i] > mat[u][i]) dis[i] = mat[u][i];
}
return ret;
} bool ans[MAXN];
int ans_pw, ans_ew; void dfs(int dep, int cnt, int sum_pw) {
if(cnt == m) {
int sum_ew = prim(dep - );
if(ans_ew == INF || ans_ew * sum_pw > ans_pw * sum_ew) {//ans_ew/ans_pw > sum_ew/sum_pw
for(int i = ; i <= n; ++i) ans[i] = use[i];
ans_ew = sum_ew; ans_pw = sum_pw;
}
return ;
}
if(dep == n + ) return ;
use[dep] = true;
dfs(dep + , cnt + , sum_pw + weight[dep]);
use[dep] = false;
dfs(dep + , cnt, sum_pw);
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
for(int i = ; i <= n; ++i) scanf("%d", &weight[i]);
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) scanf("%d", &mat[i][j]);
}
ans_ew = INF; ans_pw = ;
dfs(, , );
bool flag = false;
for(int i = ; i <= n; ++i) {
if(!ans[i]) continue;
if(flag) printf(" ");
else flag = true;
printf("%d", i);
}
printf("\n");
}
}

HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. hdu 2489 Minimal Ratio Tree

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

  7. HDU 2487 Ugly Windows(暴力)(2008 Asia Regional Beijing)

    Description Sheryl works for a software company in the country of Brada. Her job is to develop a Win ...

  8. HDU 2494/POJ 3930 Elevator(模拟)(2008 Asia Regional Beijing)

    Description Too worrying about the house price bubble, poor Mike sold his house and rent an apartmen ...

  9. HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

    Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...

随机推荐

  1. input和div模仿select,带输入提示

    有时候我们需要select和input的结合体,即可以使用下拉框,同时也可以用来输入,输入的同时显示可选的下拉选项 先上html代码 <div class="input-group i ...

  2. js 变速动画函数

    //获取任意一个元素的任意一个属性的当前的值---当前属性的位置值 function getStyle(element, attr) { return window.getComputedStyle ...

  3. JavaScript数组处理方法

    JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组 var arr2 = new Array(20); / ...

  4. HDU 5572--An Easy Physics Problem(射线和圆的交点)

    An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  5. jQuery树形控件zTree使用小结

    作者:Fonour 字体:[增加 减小] 类型:转载 时间:2016-08-02我要评论 这篇文章主要为大家详细介绍了jQuery树形控件zTree使用方法,zTree树插件的基本使用方法,感兴趣的小 ...

  6. 『Linux基础 - 4 』linux常用命令(1)

    这篇笔记包含以下知识点: 几个概念的理解:Linux命令,控制台,终端, 终端提示符 对文件目录的操作的相关命令: 切换目录,列出目录下的文件等 对文件的操作的相关命令: 创建,删除,复制,修改,移动 ...

  7. Python交换两个变量值的函数

    方法1:(错误) def func(a,b): a,b = b,a a = 1 b = 2 func(a,b) print(a," ",b) 方法2:(正确) def func(a ...

  8. HyperLedger Fabric 1.4 单机单节点部署(10.2)

    单机单节点指在一台电脑上部署一个排序(Orderer)服务.一个组织(Org1),一个节点(Peer,属于Org1),然后运行官方案例中的example02智能合约例子,实现转财交易和查询功能.单机单 ...

  9. UNIX故障--sun m4000服务器故障硬盘更换案例

    一.故障诊断 查看messages日志c0d0t0这块盘不断报错,类型为:retryable,如下: root@gdhx # more /var/adm/messages Aug  5 16:43:0 ...

  10. AIDL 进程间通信的一个小小的总结

    需求 项目需要,将做好的项目作为一个服务提供给另一个公司.我们需要提供一个apk,所以设计到进程间交互,不得不了解一下AIDL了. 了解一下AIDL 之前准备面试的时候,或多或少的了解了一点AIDL, ...