hdu2489 Minimal Ratio Tree
题意:一个 至多 n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小
题解:枚举 m 个 点 ,然后 求 最小生成树
自己粗心。。。。WA 了 好多次……(233333 )
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const double ESP = 10e-;
const int MOD = ;
typedef long long LL;
const int MAXN = + ; int graph[MAXN][MAXN];
int node[MAXN];
int tmp[MAXN];
int dist[MAXN];
int ans[MAXN];
double ansMi;
int n,m;
bool vis[MAXN];
double prim(){
double dis = ;
memset(vis,,sizeof(vis));
int cur = ;
vis[cur] = ;
for(int i = ;i < m;i++){
dist[i] = graph[ tmp[cur] ][ tmp[i] ];
} for(int i = ;i < m-;i++){
int mi = 0x7ffffff;
int k;
for(int j = ;j < m;j++){
if(!vis[j] && dist[j] < mi){
mi = dist[j];
k = j;
}
}
cur = k;
vis[cur] = ;
dis += mi;
for(int j = ;j < m;j++){
if(!vis[j] && dist[j] > graph[ tmp[cur] ][ tmp[j] ]){
dist[j] = graph[ tmp[cur] ][ tmp[j] ];
}
}
}
return dis;
} void dfs(int v,int cnt){
if(cnt == m-){
double h = ;
for(int i = ;i < m;i++){
h += node[ tmp[i] ];
}
double b = prim();
double tt = b/h;
if(tt - ansMi < -(1e-)){
ansMi = tt;
for(int i = ;i < m;i++){
ans[i] = tmp[i];
}
}
return;
}
for(int i = v+;i<= n;i++){
tmp[cnt+] = i;
dfs(i,cnt+);
}
} int main(){
// freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&m) && n &&m){
for(int i = ;i <= n;i++){
scanf("%d",&node[i]);
}
for(int i = ;i <= n;i++){
for(int j = ;j <= n;j++){
scanf("%d",&graph[i][j]);
}
}
ansMi = 10e10;
for(int i = ;i <= n;i++){
tmp[] = i;
dfs(i,);
}
for(int i = ;i < m-;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[m-]);
}
return ;
}
hdu2489 Minimal Ratio Tree的更多相关文章
- HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu2489 Minimal Ratio Tree dfs枚举组合情况+最小生成树
#include <stdio.h> #include <set> #include <string.h> #include <algorithm> u ...
- 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 ...
- HDUOJ----2489 Minimal Ratio Tree
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 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 ...
- Minimal Ratio Tree HDU - 2489
Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...
- 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(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- mysql-5.6.17-winx64 免安装 配置
[client] default_character_set=utf8 port=3306 [mysql] # 设置mysql客户端默认字符集 default_character_set=utf8 [ ...
- Hadoop_Lucene
http://codelife.me/blog/2012/11/03/jackson-polymorphic-deserialization/ http://itindex.net/blog/2012 ...
- tomcat简介之web.xml详解(转)
http://blog.csdn.net/facepp/archive/2008/04/19/2306602.aspx 位于每个Web应用的WEB-INF路径下的web.xml文件被称为配置描述符,这 ...
- 谈谈Facebook的聊天系统架构
今天看到一份 Facebook 公司 2009 年的 Slideshow, 介绍它的聊天系统架构, 其中的一张图结构非常清晰, 所以我对这张图谈谈我的看法. Web Tier: 用 PHP 开发, 聊 ...
- IT第五天 - 循环的使用、本周总结 ★★★
IT第五天 上午 循环 1.while循环.do-while循环.switch语句块的使用 下午 编程 1.编程注释的编写 2.编程力求代码的精简,算法的优化 3.变量的优化使用 小项目 1.swit ...
- Apache 2.2 到 2.4的不同
1.权限设定方式变更 2.2使用Order Deny / Allow的方式,2.4改用Require apache2.2: Order deny,allowDeny from allapache2.4 ...
- JavaScript语言基础知识6
在前面的章节中,我们知道JavaScript代码,字符和数字值当添加,将计值转换成字符,即用户输入的数目值它们被转换为字符. 如今我们要做这种样例,我想将1和2相加: <HTML> < ...
- Jquery Ajax时 error处理 之 parsererror
Jquery Ajax时 error处理 之 parsererror 01 $.ajax({ 02 type: "POST", 03 con ...
- linux嵌入式: 实现自己的tree命令
//# cat treecmd.c #include<stdio.h> #include<dirent.h> #include<sys/stat.h> #inclu ...
- ThinkPHP文件上传类
TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...