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的更多相关文章

  1. HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】

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

  2. hdu2489 Minimal Ratio Tree dfs枚举组合情况+最小生成树

    #include <stdio.h> #include <set> #include <string.h> #include <algorithm> u ...

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

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

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

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

  5. HDUOJ----2489 Minimal Ratio Tree

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

  6. 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 ...

  7. Minimal Ratio Tree HDU - 2489

    Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...

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

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

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

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

随机推荐

  1. C陷阱与缺陷(四)

    第四章 连接 4.1 什么是连接器 C语言中的一个重要思想就是分别编译,即若干个源程序可以在不同的时候单独进行编译,然后在恰当的时候整合在一起.典型的连接器把由编译器或汇编器生成的若干个目标模块,整合 ...

  2. windows apache 配置多个服务 站点 Apache Service Monitor

    把Apache安装为多个Window NT服务 ~~~ 可以在 services.msc中查看到1. 在DOS下跳到Apache安装目录里的bin目录(~~~或者用path命令 把apache的安装目 ...

  3. 英文:known good assembly(KGA) / 中文:确认好的组装件,已知好组装件

    英文:known good assembly(KGA) / 中文:确认好的组装件,已知好组装件 正确地操作印制板装配,并可作为标准件与其它同类型装配件比较的组装.也称黄金组装.

  4. Ruby学习: 类的定义和实例变量

    ruby是完全面向对象的,所有的数据都是对象,没有独立在类外的方法,所有的方法都在类中定义的. 一.类的定义语法 类的定义以 class 关键字开头,后面跟类名,以 end标识符结尾. 类中的方法以 ...

  5. gzip解压压缩的字符串数据

    import urllib2 from StringIO import StringIO import gzip def loadData(url): request = urllib2.Reques ...

  6. BZOJ 1324: Exca王者之剑

    1324: Exca王者之剑 Description Input 第一行给出数字N,M代表行列数.N,M均小于等于100 下面N行M列用于描述数字矩阵 Output 输出最多可以拿到多少块宝石 Sam ...

  7. ssh连接Linux自动断开后再也无法连上的问题

    近期公司测试机有点抓狂,情况是这样的:用SSH登录后正常使用,但另外一部电脑也在登录,这时候会发生全部断开的情况,再也无法连接,连IP都PING不通. 今天尝试在/etc/hosts.deny 加入如 ...

  8. spark sql 基本用法

    一.通过结构化数据创建DataFrame: publicstaticvoid main(String[] args) {    SparkConf conf = new SparkConf() .se ...

  9. c++, 派生类的构造函数和析构函数 , [ 以及operator=不能被继承 or Not的探讨]

    说明:文章中关于operator=实现的示例,从语法上是对的,但逻辑和习惯上都是错误的. 参见另一篇专门探究operator=的文章:<c++,operator=>http://www.c ...

  10. iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController

    Address:http://www.outofcore.com/2011/07/ios-development-proper-use-of-initwithnibnamebundle-affects ...