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. github常见操作和常见错误

    配置git的时候会使用git config,三种配置分别为git config.git config --global.git config --system. 它们之前的优先级为(由高到低):git ...

  2. ubuntu8.04下mysql更改用户和密码

    1.最近由于系统原因重装了mysql,但是发现安装过程中没有提示设置密码. 2.修改用户名和密码步骤 A.service mysql stop    #停止mysql服务 B.sudo vim /et ...

  3. mongodb数据的导出和导入

    mongo导出表说明: root@827995de7c7f:/# mongoexport --help Usage: mongoexport <options> Export data f ...

  4. 集合之Map

    Map:存放键值对,根据键对象找对应的值对象.键不能重复!Map键不能重复,有唯一性,一般通过键找对应的的值Map集合的特点: 1.具有映射关系 2.两列 3.一列要唯一 一列可以重复 键类似于 Se ...

  5. python中正则表达式re模块详解

    正则表达式是处理字符串的强大工具,它有自己特定的语法结构,有了它,实现字符串的检索,替换,匹配验证都不在话下. 当然,对于爬虫来说,有了它,从HTML里提取想要的信息就非常方便了. 先看一下常用的匹配 ...

  6. python教程(一)·命令行基本操作

    先来了解下 "命令提示符". 等等?!既然本篇文章标题是"命令行基本操作",那怎么又说到"命令提示符"去了呢?客官莫要急,且听我说 命令提示 ...

  7. Java设计模式(22)——行为模式之状态模式(State)

    一.概述 概念 再引用网友的说通俗一点: State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif else 进行状态切换, 如果针对状态的这样判 ...

  8. 20145234黄斐《信息安全系统设计基础》第六周学习总结(Y86模拟器的安装)

    Y86模拟器的安装 由于本人的电脑有些问题,在安装的时候忽然断电导致之前的成果都没有截图. 1.安装bison和flex词法分析工具 sudo apt-get install bison flex t ...

  9. 【SQLSERVER】如何设置权限用户

    一.设置权限用户的意义 SQLSERVER 数据库有两个登录方式,一个是 Windows 身份验证方式 ,另一个是 SQLSERVER 身份验证方式(sa用户): 1, Windows 身份验证方式, ...

  10. 模拟登陆百度以及Selenium 的基本用法

    模拟登陆百度,需要依赖于selenium 模块,调用浏览器,执行python命令 先来说一下这个selenium模块啦...... 本文参考内容来自 Selenium官网 SeleniumPython ...