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++反汇编与逆向分析 小结
第一章 熟悉工作环境和相关工具 1.1 熟悉OllyDBG 操作技巧 1.2 反汇编静态分析工具 IDA(最专业的逆向工具) 快捷键 功能 Enter 跟进函数实现 ...
- redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of value
错误原因: 因为redis中已经存在了相同的key, 而且key对应的值类型并不是Set,而是SortSet(改动前):再调用smembers时,抛出此错误. 解决方法: 将原来的的key给Del掉, ...
- Git 学习(六)分支管理
Git 学习(六)分支管理 几乎每一种版本控制系统都支持分支.使用分支意味着你可以从开发主线上分离开来,然后不影响主线的同时继续工作.在很多版本控制系统中,这是个昂贵的过程,常常需要创建一个源代码目录 ...
- Objective-C:OC内部可变对象和不可变对象的深(复制)拷贝问题思考:
OC内部:可变对象和不可变对象的深(复制)拷贝问题思考: 不可变对象: 例如NSString对象,因为NSString对象是常量字符串,所以,不可以更改其内容,但是可以修改指向该字符串的指针指向 ...
- guess-number-higher-or-lower-ii
// https://discuss.leetcode.com/topic/51353/simple-dp-solution-with-explanation // https://en.wikipe ...
- go语言基础之不要操作没有合法指向的内存
1.不要操作没有合法指向的内存 示例: package main //必须有个main包 import "fmt" func main() { //没有指向内存 var p *in ...
- python内置函数和魔法函数
内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...
- Android数据适配-ExpandableListView
Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableList ...
- PS使用技巧
1. 3.缩放工具:工具栏上放大镜图标:<1>上面图标栏可以选择激活放大还是缩小.<2>按住alt键可以切换,可以点击,也可以滚动滚轮<3>ctrl加"+ ...
- javascript进行遍历
javascript进行遍历 <!doctype html> <html lang="en"> <head> <meta charset= ...