hdu 2489(状态压缩+最小生成树)
Minimal Ratio Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3899 Accepted Submission(s): 1196
Given
a complete graph of n nodes with all nodes and edges weighted, your
task is to find a tree, which is a sub-graph of the original graph, with
m nodes and whose ratio is the smallest among all the trees of m nodes
in the graph.
contains multiple test cases. The first line of each test case contains
two integers n (2<=n<=15) and m (2<=m<=n), which stands for
the number of nodes in the graph and the number of nodes in the minimal
ratio tree. Two zeros end the input. The next line contains n numbers
which stand for the weight of each node. The following n lines contain a
diagonally symmetrical n×n connectivity matrix with each element shows
the weight of the edge connecting one node with another. Of course, the
diagonal will be all 0, since there is no edge connecting a node with
itself.
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.
each test case output one line contains a sequence of the m nodes which
constructs the minimal ratio tree. Nodes should be arranged in
ascending order. If there are several such sequences, pick the one which
has the smallest node number; if there's a tie, look at the second
smallest node number, etc. Please note that the nodes are numbered from 1
.
30 20 10
0 6 2
6 0 3
2 3 0
2 2
1 1
0 2
2 0
0 0
1 2
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const int INF = ;
int graph[N][N];
int weight[N];
int state[<<N];
int a[N],result[N];;
int n,m;
int vis[N],low[N];
void input(){
for(int i=;i<=n;i++) scanf("%d",&weight[i]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&graph[i][j]);
}
}
}
bool check(int num){
int cnt =;
while(num){
if(num&) cnt++;
num = num>>;
}
if(cnt==m) return true;
return false;
}
void init(int &k){
for(int i=;i<(<<n);i++){
if(check(i)) state[k++]=i;
}
k--;
}
int prim(int n,int pos){
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
low[a[i]] = graph[pos][a[i]];
}
vis[pos] = true;
int cost = ;
for(int i=;i<n;i++){
int Min = INF;
for(int j=;j<n;j++){
if(!vis[a[j]]&&low[a[j]]<Min){
pos = a[j];
Min = low[a[j]];
}
}
vis[pos] = true;
cost +=Min;
for(int j=;j<n;j++){
if(!vis[a[j]]&&low[a[j]]>graph[pos][a[j]]) low[a[j]] = graph[pos][a[j]];
}
}
return cost;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF,n+m){
int k=;
init(k);
int min_id=N;
double _ratio = INF*1.0;
input(); for(int i=;i<=k;i++){
int num = state[i];
int t =,V=;
for(int j=;j<n;j++){
if((num>>j)&){ ///这里代表第j+1个点要选进去
a[t++] = j+;
V += weight[j+];
}
}
int cost = prim(t,a[]);
double temp1 = cost*1.0/V;
if(temp1<_ratio){
_ratio = temp1;
for(int i=;i<t;i++) result[i] = a[i];
}
}
for(int i=;i<m-;i++){
printf("%d ",result[i]);
}
printf("%d\n",result[m-]);
}
}
hdu 2489(状态压缩+最小生成树)的更多相关文章
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
- hdu 4739(状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4739 思路:状态压缩. #include<iostream> #include<cs ...
- HDU 3341 状态压缩DP+AC自动机
题目大意: 调整基因的顺序,希望使得最后得到的基因包含有最多的匹配串基因,使得所能达到的智商最高 这里很明显要用状态压缩当前AC自动机上点使用了基因的情况所能达到的最优状态 我最开始对于状态的保存是, ...
- hdu 2167(状态压缩基础题)
题意:给你一个矩阵,让你在矩阵中找一些元素使它们加起来和最大,但是当你使用某一个元素时,那么这个元素周围的其它八个元素都不能取! 分析:这是一道比较基础的状态压缩题,也是我做的第三道状态压缩的题,但是 ...
- hdu 1565(状态压缩基础题)
题意:容易理解. 分析:这是我做的状态压缩第二题,一开始超内存了,因为数组开大了,后来超时了,因为能够成立的状态就那么多,所以你应该先把它抽出来!!总的来说还是比较简单的!! 代码实现: #inclu ...
- HDU 2553 状态压缩
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 3006(状态压缩)
The Number of set Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 4033 状态压缩枚举
/* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...
- hdu 2489(枚举 + 最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 思路:由于N, M的范围比较少,直接枚举所有的可能情况,然后求MST判断即可. #include ...
随机推荐
- Java中数据类型转换&基本类型变量和对象型变量
1.Java的数据类型分为三大类 布尔型,字符型和数值型 其中数值型又分为整型和浮点型 2.Java的变量类型 布尔型 boolean 字符型 char 整型 byte,short,int,lo ...
- linux下创建用户 费元星站长
linux下创建用户(一) Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系 ...
- Eclipse 工作空间(Workspace)---Eclipse教程第07课
Eclipse 工作空间(Workspace) eclipse 工作空间包含以下资源: 项目 文件 文件夹 项目启动时一般可以设置工作空间,你可以将其设置为默认工作空间,下次启动后无需再配置: 工作空 ...
- Mongoid Paging and Iterating Over Large Collections
遍历数据库中的所有记录时,我们首先想到的是Model.all.each.但是,当数据量很大的时候(数万?),这就不怎么合适了,因为Model.all.each会一次性加载所有记录,并将其实例化成 Mo ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目5
2014-04-23 18:42 题目:设计一个在线阅读系统的数据结构. 解法:这题目太大了,我的个亲娘.显然你不可能一次加载一整本书,做到单页纸加载的粒度是很必要的.为了读书的连贯效果,预取个几页也 ...
- Python lambda介绍
在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...
- day06_01 上节回顾
1.0 extend 扩展方法及"+"的对比 "+"不会改变数组的内容,而extend会改变数组的内容 2.0 sort扩展sorted() a = [1,2, ...
- 孤荷凌寒自学python第三天 初识序列
孤荷凌寒自学python第三天 初识序列 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python的序列非常让我着迷,之前学习的其它编程语言中没有非常特别关注过序列这种类型的对象,而pyt ...
- linux c编程(一)
1 常用系统环境配置 2 使用g++编译连接,使用gdb调试 3 使用makefile组织目标文件的依赖关系 4 使用git 1 常用系统环境配置 输入法 Download setup file fo ...
- css深入理解relative
第一讲 relative和absolute相煎关系 同源性 .position:relative .position:absolute 限制作用 1.限制left/top/right/bott ...