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

时间:2021-07-19 19:01:43

Minimal Ratio Tree

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

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

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

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. 
HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

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
#include <iostream>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std; int a[],f[],p[];
int mp[][];
bool vis[];
int i,j,n,m;
double ans;
void prim() //最小生成树
{
bool vis[];
int dis[];
int sumnode,sumedge=,k;
memset(vis,,sizeof(vis));
vis[]=;
sumnode=p[a[]];
for(int i=;i<=m;i++) dis[i]=mp[a[]][a[i]];
for(int i=;i<m;i++)
{
int minn=INT_MAX;
for(int j=;j<=m;j++)
{
if (!vis[j] && dis[j]<minn)
{
minn=dis[j];
k=j;
}
}
vis[k]=;
sumedge+=minn;
sumnode+=p[a[k]];
for(int j=;j<=m;j++)
if (!vis[j] && dis[j]>mp[a[k]][a[j]])
dis[j]=mp[a[k]][a[j]];
}
double w=sumedge*1.0/sumnode;
if (w<ans) //把最优解存放在f数组中
{
ans=w;
for(int i=;i<=m;i++)
f[i]=a[i];
}
return;
}
void dfs(int k,int num)//dfs暴力枚举m个节点是哪几个存在a数组中
{
if (num==m)
{
prim();
return;
}
if (k>n) return; if (!vis[k])
{
vis[k]=;
a[num+]=k;
dfs(k+,num+);
vis[k]=;
}
dfs(k+,num);
return;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
if (n== && m==) break;
for(i=;i<=n;i++) scanf("%d",&p[i]);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&mp[i][j]);
memset(vis,,sizeof(vis));
ans=INT_MAX*1.0;
dfs(,);
for(i=;i<m;i++) printf("%d ",f[i]);
printf("%d\n",f[m]);
}
return ;
}

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)的更多相关文章

  1. HDU 2489 Minimal Ratio Tree&lpar;dfs枚举&plus;最小生成树&rpar;

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

  2. HDU 2489 Minimal Ratio Tree &lpar;dfs&plus;Prim最小生成树&rpar;

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

  3. HDU 2489 Minimal Ratio Tree(暴力&plus;最小生成树)(2008 Asia Regional Beijing)

    Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...

  4. HDU 2489 Minimal Ratio Tree 最小生成树&plus;DFS

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

  5. HDU 2489 Minimal Ratio Tree(prim&plus;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. hdu2489 Minimal Ratio Tree dfs枚举组合情况&plus;最小生成树

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

  8. Minimal Ratio Tree HDU - 2489

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

  9. HDU2489 Minimal Ratio Tree 【DFS】&plus;【最小生成树Prim】

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

随机推荐

  1. 在线PDF编辑网站http&colon;&sol;&sol;www&period;pdfescape&period;com

    网站地址:http://www.pdfescape.com 先转载一个简单介绍的文章 如果你以前很少阅读PDF文档,电脑中也没有PDF阅读器:adobe reader,foxit reader之类的软 ...

  2. &period;NET 的webservice例子

    因为项目的需要,可能会经常性的需要调用接口,或者写一些接口.现在提供一些简单的例子给大家参考 写接口: [WebServiceBinding(ConformsTo = WsiProfiles.Basi ...

  3. jQuery&period;ui autoComplete使用

    官网  http://api.jqueryui.com/autocomplete/#option-source 参考了 http://www.cnblogs.com/lwme/archive/2012 ...

  4. ashx的学习

    原文:ashx的学习 嘿嘿,今天我们休息,本来是想总结一下前两周学习的javascript和jquery,但是感觉好困哦,就没有认真地学习啦,于是做了一个小小的练习,刚开始学习html使用在项目中还是 ...

  5. 利用&lowbar;winreg模块在注册表中分析无线访问热点

    _winreg.OpenKey(key, sub_key, res, sam)     key是一个已经打开的键,或者是HKEY_CLASSES_ROOT.HKEY_CURRENT_USER.HKEY ...

  6. 在linux上安装dotnetcore

    dotnet core已经出来有一段时间了,不是什么新名词了.但这个技术,目前还是比较新的,企业也没有普遍应用.它最大的亮点就是跨平台,也就是我们写的c#代码,可以运行在linux上. 在国内学习do ...

  7. &lbrack;mvc&rsqb; 过滤器filter一览

    1.mvc的过滤器种类

  8. shiro中SSL

    对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,如果需要自动重定向到https进行访问. 首先生成数字证书,生成证书到D:\localhost.keystore 使用JDK的keyt ...

  9. js页面实时显示时间

    1.通过getMonth()实现获取月份,从0开始计数,需要+1: 2.通过getDay()实现获取星期天数,从0开始,0表示星期日: 3.通过getDate()获取日期. 4.setTimeout( ...

  10. UniGui之锱铢积累&lpar;仔细看这个文件&rpar;

    http://www.doc88.com/p-4022977294324.html 这个是Word文档