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. Effective Java2读书笔记-类和接口(三)

    第17条:要么为继承而设计,并提供文档说明,要么就禁止继承 第18条:接口优于抽象类 这两条中,提到了一个很重要的概念骨架实现.也就是说,抽象类实现接口的形式.这样的好处是,接口本来不能提供默认的实现 ...

  2. LeetCode_Subsets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  3. double类型如何保留2为小数

    double d=12.2121;string str = d.ToString("F2"); double x = 29.982;Console.WriteLine(x.ToSt ...

  4. STL中istream_iterator和ostream_iterator的基本用法

    标准程序库定义有供输入及输出用的iostream iterator类,称为istream_iterator和ostream_iterator,分别支持单一型别的元素读取和写入.使用这两个iterato ...

  5. 怎样合并排序数组(How to merge 2 sorted arrays?)

    Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...

  6. hdu 5428 The Factor(数学)

    Problem Description There is a sequence of n positive integers. Fancycoder is addicted to learn thei ...

  7. List数据集动态排序

    List<Type> datas = new List<Type>; datas = datas.OrderBy(c => c.GetType().GetProperty ...

  8. long类型在C#和C++中的异同

    C++中long是32位的整数类型.   而在C#中long是64位的,对应包装类型是Int64,int对应Int32.   显然C++中的long类型,而应该对应C#中的int,   C#调用C++ ...

  9. html中opacity的使用

    今天做项目要用到一个层背景透明,层上的内容不透明的效果 结果网上找了半天,没一个靠谱的, 最后倒是被一句话点醒了:纸烧了,纸上面的字也会没了 所以,要设2层遮罩层,看代码: .dialog_1//内容 ...

  10. 【转载】ADO.NET与ROM的比较(1):ADO.NET实现CRUD

    [转载]ADO.NET与ROM的比较(1):ADO.NET实现CRUD  转自周公 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hiberna ...