hdu2489 Minimal Ratio Tree
题意:一个 至多 n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小
题解:枚举 m 个 点 ,然后 求 最小生成树
自己粗心。。。。WA 了 好多次……(233333 )
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const double ESP = 10e-;
const int MOD = ;
typedef long long LL;
const int MAXN = + ; int graph[MAXN][MAXN];
int node[MAXN];
int tmp[MAXN];
int dist[MAXN];
int ans[MAXN];
double ansMi;
int n,m;
bool vis[MAXN];
double prim(){
double dis = ;
memset(vis,,sizeof(vis));
int cur = ;
vis[cur] = ;
for(int i = ;i < m;i++){
dist[i] = graph[ tmp[cur] ][ tmp[i] ];
} for(int i = ;i < m-;i++){
int mi = 0x7ffffff;
int k;
for(int j = ;j < m;j++){
if(!vis[j] && dist[j] < mi){
mi = dist[j];
k = j;
}
}
cur = k;
vis[cur] = ;
dis += mi;
for(int j = ;j < m;j++){
if(!vis[j] && dist[j] > graph[ tmp[cur] ][ tmp[j] ]){
dist[j] = graph[ tmp[cur] ][ tmp[j] ];
}
}
}
return dis;
} void dfs(int v,int cnt){
if(cnt == m-){
double h = ;
for(int i = ;i < m;i++){
h += node[ tmp[i] ];
}
double b = prim();
double tt = b/h;
if(tt - ansMi < -(1e-)){
ansMi = tt;
for(int i = ;i < m;i++){
ans[i] = tmp[i];
}
}
return;
}
for(int i = v+;i<= n;i++){
tmp[cnt+] = i;
dfs(i,cnt+);
}
} int main(){
// freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&m) && n &&m){
for(int i = ;i <= n;i++){
scanf("%d",&node[i]);
}
for(int i = ;i <= n;i++){
for(int j = ;j <= n;j++){
scanf("%d",&graph[i][j]);
}
}
ansMi = 10e10;
for(int i = ;i <= n;i++){
tmp[] = i;
dfs(i,);
}
for(int i = ;i < m-;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[m-]);
}
return ;
}
hdu2489 Minimal Ratio Tree的更多相关文章
- HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu2489 Minimal Ratio Tree dfs枚举组合情况+最小生成树
#include <stdio.h> #include <set> #include <string.h> #include <algorithm> u ...
- 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 ...
- HDUOJ----2489 Minimal Ratio Tree
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 ...
- Minimal Ratio Tree HDU - 2489
Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...
- 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(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- cocos2dx进阶学习之CCNode
继承关系 CCNode -> CCObject CCNode在cocos2dx中抽象舞台对象,需要渲染的对象都是从CCNode派生,包括CCScene,CCLayer,CCSprite等等 C ...
- CUDA samples 第三章 sample reference 概况
示例代码分为下列几类: 1. Simple Reference 基础CUDA示例,适用于初学者, 反应了运用CUDA和CUDA runtime APIs的一些基本概念. 2. Utilitie ...
- mysql语句:批量更新多条记录的不同值
mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 1 UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_va ...
- SQL Server SQL高级查询语句小结(转)
--select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select disti ...
- POJ 1692 Crossed Matchings(DP)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- CSS3中的border-radius
以前在CSS2的基础上做圆角还能算得上是门学问!!各种图片.各种嵌套(<精通CSS——高级web标准解决方案>中有介绍,过程在这就不说了,网上一查就查得到,总之就是:没用CSS3之前觉得很 ...
- B - 最大报销额
注意超时问题,一个题可能有很多种方法解决,但是想到解决方法的同时一定要考虑这个方法的复杂度,特别是对于acm的题,有可能出现超时的情况,很浪费时间 正式比赛中就很遗憾,血的教训. 下面贴上超时的代码并 ...
- ios改变系统默认样式
iso系统下默认不能修改submit样式,于是加上如下属性就可以修改: -webkit-appearance:none; -moz-appearance:none; 先记录下.
- Codeforces 489A SwapSort
这题第一次看的时候以为是区间替换,后来发现看错了,只是单纯的元素替换. 解题思路: 先对输入的序列加个数组排个序 遍历下来,如果和排序后的结果当前元素不同,设当前位置为 i, 则往下面找,设查找位置为 ...
- VC++共享数据段实现进程之间共享数据
当我写了一个程序,我希望当这个程序同时运行两遍的时候,两个进程之间能共享一些全局变量,怎么办呢?很简单,使用VC\VC++的共享数据段.; #pragma data_seg()//恢复到正常段继续编程 ...