HDUOJ----2489 Minimal Ratio Tree
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
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.
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.
#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的更多相关文章
- HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
- HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)
Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- HDU 2489 Minimal Ratio Tree 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 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 ...
- HDU 2489 Minimal Ratio Tree(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 2489 Minimal Ratio Tree
http://acm.hdu.edu.cn/showproblem.php?pid=2489 这道题就是n个点中选择m个点形成一个生成树使得生成树的ratio最小.暴力枚举+最小生成树. #inclu ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- Minimal Ratio Tree HDU - 2489
Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...
- hdu2489 Minimal Ratio Tree
hdu2489 Minimal Ratio Tree 题意:一个 至多 n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...
随机推荐
- 什么是C++虚函数、虚函数的作用和使用方法
我们知道,在同一类中是不能定义两个名字相同.参数个数和类型都相同的函数的,否则就是“重复定义”.但是在类的继承层次结构中,在不同的层次中可以出现名字相同.参数个数和类型都相同而功能不同的函数.例如在例 ...
- HTML中显示特殊字符,如尖括号 “<”,">"等等
这几天,做个网页,之后进行解析,总是出错.最后发现是因为错误的使用了尖括号 “<”,">". 如下面的html代码: <body> 显示:<inpu ...
- Byedance AI Camp-笔试题目
最小m段和问题:给定n个整数组成的序列,现在要求将序列分割为m段,每段子序列中的数在原序列中连续排列.如何分割才能使这m段子序列的和的最大值达到最小? Input 第一行输入一个整数t,代表有t组测试 ...
- AngularJS driective 封装 自动滚动插件
1.ui-smooth-scroll.js文件内容 angular.module('app') .directive('uiSmoothScroll', ['$location', '$anchorS ...
- 独特的deadlock(仅update语句也能造成死锁)
最近遇到了一个看上去很奇怪,分析起来很有意思的死锁问题.这个死锁看上去难以理解.而分析过程中,又使用了很多分析SQL Server死锁的典型方法.记录下来整个分析过程还是很有意义的. 问题重现步骤: ...
- file not found app文件
昨天svn迁移.然后又一次check out之后编译遇到这个错误. Ld Build/Products/Debug-iphonesimulator/wiseCloudCrmTests.xctest/w ...
- 图解Eclipse或者SpringSource Tool Suite 创建模块化Maven项目
第一步:Package Explorer里选择右键,新建Maven项目,如果没有在Other里找,还没有确认一下是否安装了Maven插件 第二步:在Wizards向导里可以通过搜索,找到Maven P ...
- Untracked Files Prevent Checkout move or commit them before checkout
点开View Files... 查看里面的文件名称,在项目的.idea文件夹中删掉ViewFiles显示的文件夹名称就好
- 纯 html 以及 js 多域名跳转
<!--将以下的 endv.cn 改成要跳转的域名-->第一种:单域名的跳转 1:域名在服务器端跳转 Response.Redirect(http://endv.cn) Response. ...
- DockPanel 类
DockPanel 类 .NET Framework 4.5 其他版本 此主题尚未评级 - 评价此主题 定义您可水平或垂直排列子元素的区域,互相. 继承层次结构 System.Obje ...