题意:

      给你n个点,和任意两点的距离,让你在这N个点中找到一个有m个点并且ratio最小的树.

                        ratio = sum(edge) / sum(node)

思路: N <= 15 直接DFS暴力枚举出 m个点,然后再这m个点中跑一边最小生成树,这m个点的sum(node) 可以直接加出来,而 sum(edge) 就是最小生数的值,然后求出ratio更新最小,记录答案. 


#include<stdio.h>
#include<string.h>
#include<algorithm> #define N 20
#define inf 100000000;

using namespace
std; typedef struct
{
int
a ,b ,c;
}
NODE; bool camp(NODE a ,NODE b)
{
return
a.c < b.c;
}
NODE node[N*N];
int
map[N][N] ,weight[N];
int
ans_num[N] ,now[N] ,n ,nn;
int
mer[N];
double
now_min; int finds(int x)
{
if(
x == mer[x]) return x;
return
mer[x] = finds(mer[x]);
} void
DFS(int s ,int t)
{
if(
t == n + 1)
{
int
tmp = 0 ,sum1 = 0 ,sum2 = 0 ,mm = 0;
for(int
i = 1 ;i <= n ;i ++)
{

sum1 += weight[now[i]];
for(int
j = i + 1 ;j <= n ;j ++)
{

node[++tmp].a = now[i];
node[tmp].b = now[j];
node[tmp].c = map[now[i]][now[j]];
if(
mm < now[i]) mm = now[i];
if(
mm < now[j]) mm = now[j];
}
}

sort(node + 1 ,node + tmp + 1 ,camp);
for(int
i = 1 ;i <= mm ;i ++)
mer[i] = i;
mm = 0;
for(int
i = 1 ;i <= tmp ;i ++)
{
int
x = finds(node[i].a);
int
y = finds(node[i].b);
if(
x == y) continue;
mer[x] = y;
sum2 += node[i].c;
if(++
mm == n - 1) break;
}

//printf("%d %d %d %d\n" ,sum1 ,sum2 ,now[1] ,now[2]);
double nowm = sum2 * 1.0 / sum1;
if(
nowm < now_min)
{

now_min = nowm;
for(int
i = 1 ;i <= n ;i ++)
ans_num[i] = now[i];
}
return ;
} for(int
i = s + 1 ;i <= nn ;i ++)
{

now[t] = i;
DFS(i ,t + 1);
}
} int main ()
{
int
i;
while(
scanf("%d %d" ,&nn ,&n) && n + nn)
{
for(
i = 1 ;i <= nn ;i ++)
scanf("%d" ,&weight[i]);
for(
i = 1 ;i <= nn ;i ++)
for(int
j = 1 ;j <= nn ;j ++)
scanf("%d" ,&map[i][j]);
now_min = inf;
DFS(0 ,1);
for(
i = 1 ;i < n ;i ++)
printf("%d " ,ans_num[i]);
printf("%d\n" ,ans_num[i]);
}
return
0;
}

       

hdu2489-DFS+最小生成树的更多相关文章

  1. 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)

    1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...

  2. 【wikioi】1002 搭桥(dfs+最小生成树)

    http://wikioi.com/problem/1002/ 今天开始又开始刷水了哈T_T.照着hzwer神犇的刷题记录刷!!! 题解: 一开始我也不会,但是我想到了直接爆搜T_T. 好吧,题解. ...

  3. 【POJ 1639】 Picnic Planning (最小k度限制生成树)

    [题意] 有n个巨人要去Park聚会.巨人A和先到巨人B那里去,然后和巨人B一起去Park.B君是个土豪,他家的停车场很大,可以停很多车,但是Park的停车场是比较小.只能停k辆车.现在问你在这个限制 ...

  4. Code VS 1002 搭桥

    题目描述 Description 有一矩形区域的城市中建筑了若干建筑物,如果某两个单元格有一个点相联系,则它们属于同一座建筑物.现在想在这些建筑物之间搭建一些桥梁,其中桥梁只能沿着矩形的方格的边沿搭建 ...

  5. 【牛客提高训练营5B】旅游

    题目 吉老师的题时过一年还是不会做 从\(1\)号点出发经过每条边至少一次并且还要回到\(1\)号点,这跟欧拉回路的条件非常像,但是欧拉回路的实际上是"经过每一条边恰好一次并且回到出发点&q ...

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

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

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

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

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

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  9. HDU 5723 Abandoned country (最小生成树 + dfs)

    Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  10. HDU 5723 Abandoned country (最小生成树+dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

随机推荐

  1. python学习之常用数据结构

    前言:数据结构不管在哪门编程语言之中都是非常重要的,因为学校的课程学习到了python,所以今天来聊聊关于python的数据结构使用. 一.列表 list 1.列表基本介绍 列表中的每个元素都可变的, ...

  2. 剑指 Offer 59 - I. 滑动窗口的最大值 + 双指针 + 双端队列

    剑指 Offer 59 - I. 滑动窗口的最大值 Offer_59_1 题目详情 方法一:暴力方法+双指针 package com.walegarrett.offer; /** * @Author ...

  3. pytorch(17)学习率调整

    学习率调整 class _LRScheduler 主要属性 optimizer:关联的优化器 last_epoch:记录epoch数 bash_lrs:记录初始学习率 class _LRSchedul ...

  4. linux下redis安装运行教程——redis系列

    天没降大任于我,照样苦我心智,劳我筋骨. 安装运行的过程 由于官网太慢,csdn里的资源又要钱,所以呢,只能使用我自己本地以前下载的陈年..哦不,3.xredis安装包 资源已经放到百度云,需要的可以 ...

  5. Python基础(1)——变量和数据类型[xiaoshun]

    目录 一.变量 1.概述 Variables are used to store information to be referenced(引用)and manipulated(操作) in a co ...

  6. 两种常见Content-type的方便理解

    application/x-www-form-urlencoded:key=value键值对application/json:{name:"张三"} JSON字符串塞到请求的bod ...

  7. 2019 GDUT Rating Contest III : Problem A. Out of Sorts

    题面: 传送门 A. Out of Sorts Input file: standard input Output file: standard output Time limit: 1 second M ...

  8. javascript 最权威的知识点总结

    JavaScript中如何检测一个变量是一个String类型?请写出函数实现typeof(obj) === "string"typeof obj === "string& ...

  9. python课程设计--学生管理系统

    系统要求 1.添加学生 2.删除学生 3.修改学生信息 4.查询学生 5.查看所有学生信息 6.学生信息数据的存储与读取 源码:student.py #coding:utf-8 2 #定义学员类 3 ...

  10. 二叉树的建立与遍历(c语言)入门

    树其实在本质上就是一对多,链表就是一对一. 二叉树的建立: 这里的代码采用的是最粗暴的创建方法,无实际用处.但初次学习二叉树可以通过这个创建方法更好的理解二叉树. 二叉树的遍历: 遍历在大体上分为递归 ...