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. 0课程介绍(Week1,3月3日)

    一.自我介绍 1.姓名:杨晔 2.办公室:B211-2 3.电子邮件:yangye@zjjy.com.cn 4.QQ:6706892 5.博客:http://www.cnblogs.com/meety ...

  2. qt实现-给SQLITE添加自定义函数

    需要使用sqlite里的password对某个字段进行加密,由于使用的sqlite是由QT封装好的QSqlDatabase,没有发现加载扩展函数的方法,所以自己实现了一个. 在网上也没找到相应的参考, ...

  3. python and 和 or

    在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样.但是它们并不返回布尔值,而是返回它们实际进行比较的值之一. 例 4.15. and 介绍 >>> 'a' a ...

  4. hdu 4730 We Love MOE Girls

    http://acm.hdu.edu.cn/showproblem.php?pid=4730 直接用string类处理字符串. AC代码: #include<iostream> #incl ...

  5. C语言,单链表操作(增删改查)(version 0.1)

    这天要面试,提前把链表操作重新写了一遍.备份一下,以备不时之需. 希望有人能看到这篇代码,并指正. // File Name : list.h #include "stdafx.h" ...

  6. 自定义UIViewController与xib文件关系深入分析

    6月14日 上海 OSC 源创会开始报名啦,有很多机械键盘送哦!!! 用xcode模板向工程加入UIViewController sub class的时候,如果选中了with xib for inte ...

  7. 关于PHPExcel类占用内存问题

    最近在帮一家公司做后台excel导出功能,使用的工具类是phpexcel,因为这个类功能比较强大.全面. 但是遇到下面一个问题: 当导出数据量达到一定数量级的时候,比如说1000条,服务器出现卡顿.白 ...

  8. Python 第十三篇之二:jQuery基础

    一:jQuery是一个兼容多浏览器的javascript类库,核心理念是write less,do more(写得更少,做得更多),对javascript进行了封装,是的更加便捷的开发,并且在兼容性方 ...

  9. Core dotnet 命令大全

    Core dotnet 命令大全 dotnet 命令大全,让你理解dotnet 命令. 本文将以一个实例串起 dotnet 所有命令,让你玩转dotnet 命令. 本篇文章编写环境为windows 1 ...

  10. QT5 动态链接库的创建和使用(QT自己做动态库给自己使用)

    记录一下QT5 动态链接库的创建和使用 在文章的最后有完成的代码供下载 1.创建动态链接库 先新建一个库项目 选择chose进入下一下页面,类型选择共享库,输入一个名称:我输入的是sld 再点击下一步 ...