题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小。

思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ G++里面需要cstring头文件。

#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <memory>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std; const int MAXN = 100;
const int INF = 1<<30; #define CLR(x,y) memset(x,y,sizeof(x))
#define MIN(m,v) (m)<(v)?(m):(v)
#define MAX(m,v) (m)>(v)?(m):(v)
#define ABS(x) ((x)>0?(x):-(x))
#define rep(i,x,y) for(i=x;i<y;++i) int n,m,k; double ans = 0; int select[MAXN];
int g[MAXN][MAXN];
int val[MAXN];
int visit[MAXN];
double dist[MAXN];
int ind[MAXN]; double Prim()
{
int i,j,tmp,mark_i;
int mark_min;
int sum_node = 0;
int sum_edge = 0; for(i = 0; i < n; ++i)
{
dist[i] = INF;
visit[i] = 0;
} for(i = 0; i < n; ++i)
if(select[i]>0)
{
dist[i] = 0;
break;
} int cnt = 0; for(i = 0; i < n; ++i,++cnt)
{
if(cnt>=m) break; mark_min = INF; for(j = 0; j < n; ++j)
{
if(select[j]>0 && !visit[j] && mark_min>dist[j])
{
mark_min = dist[j];
mark_i = j;
}
} visit[mark_i] = 1; sum_edge += dist[mark_i]; for( j = 0; j < n; ++j)
{
if(visit[j]==0 && select[j]>0 && dist[j] > g[mark_i][j])
dist[j] = g[mark_i][j];
}
} for( i = 0; i < n; ++i)
if(select[i] > 0 )
sum_node += val[i]; return double(sum_edge)/sum_node; } int DFS(int cur, int deep)
{
double res = INF; select[cur] = 1; if(deep < m)
{
for(int i = cur+1; i < n; ++i)
{
DFS(i,deep+1);
}
} if(deep == m)
{
res = Prim();
if(res < ans)
{
ans = res;
for(int i = 0; i < n; ++i)
ind[i] = select[i]; }
} select[cur] = 0; return 0;
} int Solve()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n == 0 && m == 0) break;
for(int i = 0 ; i < n; ++i)
scanf("%d",&val[i]);
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
{
scanf("%d",&g[i][j]);
} CLR(select,0);
ans = INF; for(int i = 0 ; i < n; ++i)
DFS(i,1); int tag = 0;
for(int i = 0; i < n; ++i)
if(ind[i] > 0)
if(tag == 0)
{
printf("%d",i+1);
tag = 1;
}
else
printf(" %d",i+1);
printf("\n");
}
return 0;
} int main()
{
Solve(); return 0;
}

{POJ}{3925}{Minimal Ratio Tree}{最小生成树}的更多相关文章

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

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

  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(暴力+最小生成树)(2008 Asia Regional Beijing)

    Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...

  4. HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】

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

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

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

  6. hdu2489 Minimal Ratio Tree

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

  7. HDUOJ----2489 Minimal Ratio Tree

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

  8. Minimal Ratio Tree HDU - 2489

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

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

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

随机推荐

  1. VR定制 AR定制 就找北京动软VR开发团队(VR案例 AR案例)

    我们长期承接丰交互软件.游戏项目外包: VR/AR内容应用定制.VR.AR游戏项目外包(有主流测试硬件设备) VR全景应用.视频外包 请提供贵公司的信息,我们将提供高大上的VR案例欢迎联系我们给您提供 ...

  2. 基于WebDriverAgent代理服务,实现iOS手机app自动化测试的框架搭建

    iOS自动化测试一直使用的appium,iOS系统升级至10.0 Xcode8.0之后, 改用WebDriverAgent代理服务作为server,编写了一套基于WebDriverAgent服务 ap ...

  3. Activity Intent Flags及Task相关属性

    转自http://www.cnblogs.com/lwbqqyumidi/p/3775479.html 今天我们来讲一下Activity的task相关内容. 上次我们讲到Activity的四种启动模式 ...

  4. 多个Python环境的构建:基于virtualenv 包

    假如一台计算中安装多个Python版本,而不同版本可能会pip安装不同的包,为了避免混乱,可以使用virtualenv包隔离各个Python环境,实现一个Python版本对应一套开发环境. 本地概况: ...

  5. HBase最佳实践-列族设计优化

    本文转自hbase.收藏学习下. 随着大数据的越来越普及,HBase也变得越来越流行.会用HBase现在已经变的并不困难,然而,怎么把它用的更好却并不简单.那怎么定义'用的好'呢?很简单,在保证系统稳 ...

  6. JS获取地址栏参数的方法

    1. window.location.href 2.正则方法 function getUrlParam(name) { var reg = new RegExp("(^|&)&quo ...

  7. G不可失

    html和css部分和引用的库 <!DOCTYPE html><html lang="en"><head> <meta charset=& ...

  8. Frameset的使用

    一.frameset 1. 属性 ①border 设置框架的边框粗细. ②bordercolor 设置框架的边框颜色. ③frameborder 设置是否显示框架边框.设定值只有0.1:0 表示不要边 ...

  9. C/C++二维数组分配内存

    //C++方式 double **Q=new double*[row];    //初始化Q矩阵 for(int i=0;i<row;++i) Q[i]=new double[POS_NUM]( ...

  10. reverse iterator

    Problem 1: vector<int> coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector<int>::const_iterator ...