hdu 2489 Minimal Ratio Tree
http://acm.hdu.edu.cn/showproblem.php?pid=2489
这道题就是n个点中选择m个点形成一个生成树使得生成树的ratio最小。暴力枚举+最小生成树。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1000
using namespace std;
const int inf=<<;
const double eps=1e-; int map[maxn][maxn];
int g[maxn][maxn];
bool vis[maxn];
int dis[maxn];
int node[maxn];
int ok[maxn];
int w[maxn];
int n,m;
double ans; int prime()
{
memset(vis,false,sizeof(vis));
for(int i=; i<=m; i++) dis[i]=g[][i];
dis[]=;
vis[]=true;
int sum1=;
for(int i=; i<m; i++)
{
int x,m2=inf;
for(int y=; y<=m; y++) if(!vis[y]&&dis[y]<m2) m2=dis[x=y];
vis[x]=true;
sum1+=m2;
for(int y=; y<=m; y++) if(!vis[y]&&dis[y]>g[x][y]){
dis[y]=g[x][y];
}
}
return sum1;
} void dfs(int u,int cnt)
{
node[cnt]=u;
if(cnt>=m)
{
for(int i=; i<=m; i++)
{
for(int j=; j<=m; j++)
{
g[i][j]=map[node[i]][node[j]];
}
}
double edgew=prime()*1.0;
double nodew=0.0;
for(int i=; i<=m; i++)
{
nodew+=w[node[i]];
}
double sum=edgew/nodew;
if(ans-sum>=eps)
{
ans=sum;
for(int i=; i<=m; i++)
ok[i]=node[i];
}
}
for(int j=u+; j<=n; j++)
{
dfs(j,cnt+);
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(node,,sizeof(node));
memset(map,,sizeof(map));
memset(w,,sizeof(w));
memset(ok,,sizeof(ok));
if(n==&&m==) break;
for(int i=; i<=n; i++)
scanf("%d",&w[i]);
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&map[i][j]);
}
}
ans=inf;
for(int i=; i<=n; i++)
{
dfs(i,);
}
for(int j=; j<=m; j++)
{
if(j==) printf("%d",ok[j]);
else printf(" %d",ok[j]);
}
printf("\n");
}
return ;
}
hdu 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(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 个 点 ,然后 求 最小生成树 ...
- HDUOJ----2489 Minimal Ratio Tree
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- Android开源库
http://blog.csdn.net/xiaanming/article/details/9470223 一.兼容类库 ActionBarSherlock : Action Bar是Android ...
- LeetCode_Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- gnuplot
一. 简介 gnuplot是一个命令行驱动的科学绘图工具,可将数学函数或数值资料以平面图或立体图的形式画在不同种类终端机或绘图输出装置上.gnuplot既支持命令行交互模式,也支持脚本. 二. 特性 ...
- hadoop 2.3 集群总结
用了近两个礼拜的摸索终于搭建好了hadoop集群,测试性能也符合预期. centos6.4下hadoop2.3集群总结如下: 关于环境的设置: 1.关闭selinux (反复折腾了好多次) vi /e ...
- 利用jquery表格添加一行并在每行第一列大写字母显示实现方法
表格添加一行并在每行第一列大写字母显示jquery实现方法 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN& ...
- Live555 分析(一):类介绍
从程序的结构来看,live项目包括了四个基本库.程序入口类(在mediaServer中)和一些测试代码(在testProgs中). 四个基本静态库是UsageEnvironment.BasicUsag ...
- C#模拟网站用户登录
我们在写灌水机器人.抓资源机器人和Web网游辅助工具的时候第一步要实现的就是用户登录.那么怎么用C#来模拟一个用户的登录拉?要实现用户的登录,那么首先就必须要了解一般网站中是怎么判断用户是否登录的. ...
- iOS绘图教程
本文是<Programming iOS5>中Drawing一章的翻译,考虑到主题完整性,翻译版本中加入了一些书中未涉及到的内容.希望本文能够对你有所帮助.(本文由海水的味道翻译整理,转载请 ...
- EffectiveC#02--仅在对基类进行强制更新时才使用new修饰符
1.建议避免使用new修饰符来重新定义非虚函数. 非虚方法是静态绑定的,不管哪里的代码也不管在哪里引用, 它总是严格的调用类中所定义的函数.并不会在运行时在 派生类中查找不同的版本. 2.何时使用ne ...
- webservice 远程调试配置
在.NET 中已经默认将WEBSERVICE的远程调试功能关闭,有的时候我们需要远程调试程序的时候,就需要打开此功能我们只需在WEBSERVICE的项目的中添web.config的<system ...