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 ...
随机推荐
- ng-bind-html 的使用
AngualrJS 提供了指令ng-bind-html 用于绑定包含HTML标签的文档,使用方式: <ANY ng-bind-html=""> ... </ANY ...
- Populating Next Right Pointers in Each Node II 解答
Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...
- Hdu4742-Pinball Game 3D(cdq分治+树状数组)
Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pinball ...
- 【CF 189A Cut Ribbon】dp
题目链接:http://codeforces.com/problemset/problem/189/A 题意:一个长度为n的纸带,允许切割若干次,每次切下的长度只能是{a, b, c}之一.问最多能切 ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- sql求和涉及到null值
SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 "Products" 表: P_Id ProductName UnitPrice ...
- 简易的C/S系统(实现两个数的和)
//Client:#include <string.h> #include <sys/socket.h> #include <stdio.h> #include & ...
- vs2013中国集
在TOOLS的菜单条下的最后一项.进去后在输入框输入Language.按Enter.选择语言,然后确定就可以. 如图 假设没有点击下拉框底下的链接 就会调挑转到语言包下载界面 下载须要的语言就可以, ...
- Dalvik虚拟机的启动过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8885792 在Android系统中,应用程序进 ...
- Hibernate PO对象的状态
Hibernate的PO对象有三种状态:临时状态(又称临时态).持久状态(又称为持久态)和脱管状态(又称为脱管态.游离态).处理持久态的对象也称为PO,临时对象和脱管对象也称为VO. 1.临时态: 简 ...