Description
Input
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
题目大意:给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)的更多相关文章
-
HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
-
HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)
Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
-
HDU 2489 Minimal Ratio Tree 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
-
HDU 2489 Minimal Ratio Tree(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
-
HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
-
hdu 2489 Minimal Ratio Tree
http://acm.hdu.edu.cn/showproblem.php?pid=2489 这道题就是n个点中选择m个点形成一个生成树使得生成树的ratio最小.暴力枚举+最小生成树. #inclu ...
-
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 ...
-
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 ...
-
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 ...
随机推荐
-
MYSQL 开发技巧
主要涉及:JOIN .JOIN 更新.GROUP BY HAVING 数据查重/去重 1 INNER JOIN.LEFT JOIN.RIGHT JOIN.FULL JOIN(MySQL 不支持).CR ...
-
yii2权限控制rbac之菜单menu最详细教程
前面我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程中完美实现了yii2的后台搭建和rbac权限控制,如果你还没有实现,请先看上文再回来参考本文,因为本文是在上文的基础上进行完善和补充. ...
-
关于环境变量PATH的几点注意事项
查看执行文件路径变量PATH的内容可用echo $PATH.echo表示显示打印之意,$表示后接的是变量. 如下图所示,其中每个目录中间用冒号(:)来隔开,每个目录是有顺序之分的: 如果预修改PATH ...
-
Android-onInterceptTouchEvent()和onTouchEvent()总结
老实说,这两个小东东实在是太麻烦了,很不好懂,我自己那api文档都头晕,在网上找到很多资料,才知道是怎么回事,这里总结一下,记住这个原则就会很清楚了: 1.onInterceptTouchEvent( ...
-
走进spring之springmvc
走进spring之springmvc 在动手之前,我们需要了解下springnvc.这里先献上一张springmvc的流程图及讲解. Spring的MVC框架是一个基于DispatcherServle ...
-
教你做一个单机版人事管理系统(Winform版)treeview与listview使用详情
------------------------------------------------------------------部门部分------------------------------ ...
-
用JNDI连接数据库
之前说到了利用Java中的Properties类读取properties配置文件,连接数据库,现在说另一种方法,他们的目的和作用都是一样的,都是为了提高代码的复用性,解决了更改数据库 时还要更改代码的 ...
-
cron表达式总结
cron表达式用于配置cronTrigger的实例,在定时任务中会用到cron表达式.cron表达式实际上是由七个子表达式组成.这些表达式之间用空格分隔. 可通过工具校验:http://cron.qq ...
-
RBMQ发布和订阅消息
RBMQ发布和订阅消息 exchange 参考翻译自: RabbitMQ官网 生产者并非将消息直接发送到queue,而是发送到exchange中,具体将消息发送到特定的队列还是多个队列,或者是丢弃,取 ...
-
DevExpress03、GridControl
设计数据源并绑定字段: 数据源可以是实现下列接口之一的任何类型: IList 接口,包括一维数组.List<T>等! IListSource 接口,例如,DataTable 和 DataS ...