Minimal Ratio Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2180    Accepted Submission(s): 630

Problem Description
For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation.

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.

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

 
Output
For 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 .
 
Sample Input
3 2
30 20 10
0 6 2
6 0 3
2 3 0
2 2
1 1
0 2
2 0
0 0
 
Sample Output
1 3
1 2
 
Source
 
 
 
这道题是2008年北京现场比赛的一道题,题意大致意思是给定n个节点的一个图,要你从中选出这小边的权值和除以节点权值和的最小的一个树
于是很好理解的为最小生成树,采用普利姆最小生成树....注意精度的问题,这里我wa了n次
哎,喵了个咪
代码:
 #include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define max 0x3f3f3f3f
#define maxn 17
int node_weight[maxn];
int edge_weight[maxn][maxn];
int depath[maxn]; //以这些点形成一颗最小生成树
int m , n ;
double res;
int stu[maxn];
int sub_map[maxn][maxn];
void Prime()
{
int vis[maxn]={};
int lowc[maxn];
int i,j,k,minc;
double ans=;
for(i=;i<=m;i++) //从n中挑出m个点形成一个子图
{
for(j=;j<=m;j++)
{
if(edge_weight[depath[i]][depath[j]]==)
sub_map[i][j]=max;
else
sub_map[i][j]=edge_weight[depath[i]][depath[j]];
}
}
vis[]=;
for(i=;i<=m;i++)
{
lowc[i]=sub_map[][i];
}
for(i=;i<=m;i++)
{
minc=max;
k=;
for(j=;j<=m;j++)
{
if(vis[j]==&&minc>lowc[j])
{
minc=lowc[j];
k=j;
}
}
if(minc==max) return ; //表示没有联通
ans+=minc;
vis[k]=;
for(j= ; j<=m;j++)
{
if(vis[j]==&&lowc[j]>sub_map[k][j])
lowc[j]=sub_map[k][j];
}
}
int sum=;
for(i=;i<=m;i++) //统计点权值的和
sum+=node_weight[depath[i]];
ans/=sum;
if(res+0.00000001>=ans)
{
if((res>=ans&&res<=ans+0.000001)||(res<=ans&&res+0.000001>=ans+0.000001))
{
for(i=;i<=m;i++)
{
if(stu[i]<depath[i]) return;
}
}
res=ans;
memcpy(stu,depath,sizeof(depath));
}
}
void C_n_m(int st ,int count)
{
if(count==m)
{
Prime();
return ;
}
for(int i=st ;i<=n;i++ )
{
depath[count+]=i;
C_n_m(i+,count+);
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m),m+n)
{
for(i=;i<=n;i++)
scanf("%d",node_weight+i); //记录节点权值
for(i=;i<=n;i++) //记录边权值
for(j=;j<=n;j++)
scanf("%d",&edge_weight[i][j]);
// C(n,m)
res=max;
C_n_m(,);
for(i=;i<=m;i++)
{
printf("%d",stu[i]);
if(i!=m)printf(" ");
}
putchar();
}
return ;
}

HDUOJ----2489 Minimal Ratio Tree的更多相关文章

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

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

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

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

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

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

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

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

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

  6. hdu 2489 Minimal Ratio Tree

    http://acm.hdu.edu.cn/showproblem.php?pid=2489 这道题就是n个点中选择m个点形成一个生成树使得生成树的ratio最小.暴力枚举+最小生成树. #inclu ...

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

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

  8. Minimal Ratio Tree HDU - 2489

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

  9. hdu2489 Minimal Ratio Tree

    hdu2489 Minimal Ratio Tree 题意:一个 至多  n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...

随机推荐

  1. Android之从Browser中打开本地的应用程序&微信检测是否有对应app

    在对应的应用程序的AndroidManifest.xml中配置: <activity android:name=".ui.TabHostActivity" android:w ...

  2. VS2010+OpenCV2.4.3配置

    VS2010+OpenCV2.4.3配置:  环境变量path: D:\openCV2.4.3\opencv\build\x86\vc10\bin  项目-属性-VC++目录:(vs2008中,工具- ...

  3. 学习笔记:AC自动机

    话说AC自动机有什么用......我想要自动AC机 AC自动机简介:  首先简要介绍一下AC自动机:Aho-Corasick automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配 ...

  4. MySQL命令行查询结果中文显示乱码

    数据库编码格式为utf8,表和字段也都是utf8,存进去的格式是utf-8 但是用命令行工具查询命令select * from 表名; 查询出来的中文是乱码 原因:MySQL客户端根本就不能以utf8 ...

  5. 最小均方算法(LMS Algorithm)理论及DSP实现

    LMS算法可认为是机器学习里面最基本也比较有用的算法,神经网络中对参数的学习使用的就是LMS的思想,在通信信号处理领域LMS也非常常见,比如自适应滤波器. 本文主要对LMS(Least Mean Sq ...

  6. MongoDB学习笔记(三)--权限 && 导出导入备份恢复 && fsync和锁

    权限                                                                                             绑定内网I ...

  7. SQL Server-已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行

    在更新没有设置主键的表的时候出现下图中的问题: 问题原因: 这种问题大多是由于没有主键(PK)导致同一张表中存在若干条相同的数据 DBMS存储时,只为其存储一条数据,因为DBMS底层做了优化,以减少数 ...

  8. 超全Linux备份工具集合,满足你的所有需要!

    经常备份计算机上的数据是个好的做法,它可以手动完成,也可以设置成自动执行.许多备份工具拥有不同的功能特性,让用户可以配置备份类型.备份时间.备份对象.将备份活动记入日志及执行更多操作. 1.Rsync ...

  9. Cognos开发图表乱码问题

    在此之前提到过在利用TR建模导入IQD数据源的时候遇到乱码的一种解决方案: http://www.cnblogs.com/wxjnew/p/3374029.html 今天说的是在RS中开发新报表的时候 ...

  10. PHP经典项目案例-(一)博客管理系统5

    本篇实现发表博客. 八.发表博客 (1).界面实现file.php <tr>      <td colSpan=3 valign="baseline" style ...