HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)
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学习之变量
变量 MySQL本质是一种编程语言,需要很多变量来保存数据,mysql中很多的属性控制都是通过MySQL中固有的变量来实现的. 系统变量 系统内部定义的变量,系统变量针对的是所有用户(MySQL客户端 ...
- 浅析MySQL主从复制技术(异步复制、同步复制、半同步复制)
Preface As we all know,there're three kinds of replication in MySQL nowadays.Such as,asynchr ...
- zabbix 3.x 监控日志文件
1.启用zabbix主动模式 在zabbix agent端,修改/etc/zabbix/zabbix_agentd.conf ServerActive=服务端IP Hostname=tspnginx0 ...
- Linux基础命令之文件和目录操作(一)
pwd print working directory的缩写,作用是显示当前工作目录的绝对路径,一般进行频繁切换路径时使用. -L 显示逻辑路径(或略软链接文件),不常用. -P 显示物理路径,不常用 ...
- linux系统基础之六--系统引导(基于centos7.4 1708)
- 前台页面上传data image图片,java后台接收图片保存
最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataURL,之后还要将这个图片上传到云,这个时候如何操作就不清楚了, ...
- 4 二维数组中的查找 JavaScript
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- iOS 12 真机调试 Xcode 9 提示 Could not locate device support files.
升级 iOS 12 之后,使用 Xcode 9 真机调试会提示错误: Could not locate device support files. This iPhone 6 Plus is runn ...
- BZOJ2600_ricehub_KEY
题目传送门 这道题一开始我还以为是贪心,sort一遍直接取中点然后求最优值. 但写了之后才发现错误,设置的谷仓只要是一段区间的中点即可.这段区间的两端一定是两片谷田. 所以枚举区间的左端点,二分右端点 ...
- 北京Uber优步司机奖励政策(12月9日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...