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的更多相关文章

  1. HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)

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

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

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

  3. HDU 2489 Minimal Ratio Tree 最小生成树+DFS

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

  4. 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 ...

  5. HDU 2489 Minimal Ratio Tree(prim+DFS)

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

  6. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

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

  7. Minimal Ratio Tree HDU - 2489

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

  8. hdu2489 Minimal Ratio Tree

    hdu2489 Minimal Ratio Tree 题意:一个 至多  n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...

  9. HDUOJ----2489 Minimal Ratio Tree

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

随机推荐

  1. (转)C#在父窗口中调用子窗口的过程(无法访问已释放的对象)

    C#在父窗口中调用子窗口的过程: 1. 创建子窗口对象 2. 显示子窗口对象   笔者的程序中,主窗体MainFrm通过菜单调用子窗口ChildFrm.在窗体中定义了子窗口对象,然后在菜单项点击事件中 ...

  2. 车祸 shit

    今天上班的时候就觉得右眼在那跳,妈的,果不其然,回家路上自行车也跟人家撞上了,郁闷,裤子也坏了,腿也伤了.我还没反应过来,撞一起的是个女的,十七八岁吧,郁闷,什么破自行车.强烈呼吁不要去买小自行车了, ...

  3. NOI2012 Day1

    NOI2012 Day1 随机数生成器 题目描述:给出数列\(X_{n+1}=(aX_n+c)mod m\),求\(X_n mod g\) solution: 矩阵乘法,但数有可能在运算时爆\(lon ...

  4. vijos1777 引水入城

    描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,其中每个格子都代表一座城市,每座城市都有一个海拔高度. 为了使居民们都尽可能 ...

  5. UESTC_小panpan学图论 2015 UESTC Training for Graph Theory<Problem J>

    J - 小panpan学图论 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

  6. Roman to Integer && Integer to Roman 解答

    Roman Numeral Chart V:5 X:10 L:50 C:100 D:500 M:1000 规则: 1. 重复次数表示该数的倍数2. 右加左减:较大的罗马数字右边记上较小的罗马数字,表示 ...

  7. MediaInfo源代码分析 1:整体结构

    MediaInfo 用来分析视频和音频文件的编码和内容信息,是一款是自由软件 (免费使用.免费获得源代码).之前编程的时候,都是直接调用它提供的Dll,这次突然来了兴趣,想研究一下它内部究竟是怎么实现 ...

  8. Hive 10、Hive的UDF、UDAF、UDTF

    Hive自定义函数包括三种UDF.UDAF.UDTF UDF(User-Defined-Function) 一进一出 UDAF(User- Defined Aggregation Funcation) ...

  9. ios block使用

    BlockTest.h #import <Foundation/Foundation.h> typedef void (^didFinishBlock)(NSString *str); t ...

  10. ios 以NSObject为父类的各类间继承关系