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

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

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <cmath>
  5. #include <memory>
  6. #include <algorithm>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. using namespace std;
  10.  
  11. const int MAXN = 100;
  12. const int INF = 1<<30;
  13.  
  14. #define CLR(x,y) memset(x,y,sizeof(x))
  15. #define MIN(m,v) (m)<(v)?(m):(v)
  16. #define MAX(m,v) (m)>(v)?(m):(v)
  17. #define ABS(x) ((x)>0?(x):-(x))
  18. #define rep(i,x,y) for(i=x;i<y;++i)
  19.  
  20. int n,m,k;
  21.  
  22. double ans = 0;
  23.  
  24. int select[MAXN];
  25. int g[MAXN][MAXN];
  26. int val[MAXN];
  27. int visit[MAXN];
  28. double dist[MAXN];
  29. int ind[MAXN];
  30.  
  31. double Prim()
  32. {
  33. int i,j,tmp,mark_i;
  34. int mark_min;
  35. int sum_node = 0;
  36. int sum_edge = 0;
  37.  
  38. for(i = 0; i < n; ++i)
  39. {
  40. dist[i] = INF;
  41. visit[i] = 0;
  42. }
  43.  
  44. for(i = 0; i < n; ++i)
  45. if(select[i]>0)
  46. {
  47. dist[i] = 0;
  48. break;
  49. }
  50.  
  51. int cnt = 0;
  52.  
  53. for(i = 0; i < n; ++i,++cnt)
  54. {
  55. if(cnt>=m) break;
  56.  
  57. mark_min = INF;
  58.  
  59. for(j = 0; j < n; ++j)
  60. {
  61. if(select[j]>0 && !visit[j] && mark_min>dist[j])
  62. {
  63. mark_min = dist[j];
  64. mark_i = j;
  65. }
  66. }
  67.  
  68. visit[mark_i] = 1;
  69.  
  70. sum_edge += dist[mark_i];
  71.  
  72. for( j = 0; j < n; ++j)
  73. {
  74. if(visit[j]==0 && select[j]>0 && dist[j] > g[mark_i][j])
  75. dist[j] = g[mark_i][j];
  76. }
  77. }
  78.  
  79. for( i = 0; i < n; ++i)
  80. if(select[i] > 0 )
  81. sum_node += val[i];
  82.  
  83. return double(sum_edge)/sum_node;
  84.  
  85. }
  86.  
  87. int DFS(int cur, int deep)
  88. {
  89. double res = INF;
  90.  
  91. select[cur] = 1;
  92.  
  93. if(deep < m)
  94. {
  95. for(int i = cur+1; i < n; ++i)
  96. {
  97. DFS(i,deep+1);
  98. }
  99. }
  100.  
  101. if(deep == m)
  102. {
  103. res = Prim();
  104. if(res < ans)
  105. {
  106. ans = res;
  107. for(int i = 0; i < n; ++i)
  108. ind[i] = select[i];
  109.  
  110. }
  111. }
  112.  
  113. select[cur] = 0;
  114.  
  115. return 0;
  116. }
  117.  
  118. int Solve()
  119. {
  120. while(scanf("%d%d",&n,&m)!=EOF)
  121. {
  122. if(n == 0 && m == 0) break;
  123. for(int i = 0 ; i < n; ++i)
  124. scanf("%d",&val[i]);
  125. for(int i = 0; i < n; ++i)
  126. for(int j = 0; j < n; ++j)
  127. {
  128. scanf("%d",&g[i][j]);
  129. }
  130.  
  131. CLR(select,0);
  132. ans = INF;
  133.  
  134. for(int i = 0 ; i < n; ++i)
  135. DFS(i,1);
  136.  
  137. int tag = 0;
  138. for(int i = 0; i < n; ++i)
  139. if(ind[i] > 0)
  140. if(tag == 0)
  141. {
  142. printf("%d",i+1);
  143. tag = 1;
  144. }
  145. else
  146. printf(" %d",i+1);
  147. printf("\n");
  148. }
  149. return 0;
  150. }
  151.  
  152. int main()
  153. {
  154. Solve();
  155.  
  156. return 0;
  157. }

{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. 用两个Stack来实现一个Queue

    import java.util.Stack; /** * 问题:用两个Stack来实现一个Queue; * 方法:栈的特点是先进后出:而队列的特点是先进先出: * 用两个栈正好能把顺序调过来: * ...

  2. 【uTenux】学习一个嵌入式操作系统-uTenux

    [这个是汇总] 最近有点时间,参加了EEPW论坛和悠龙咨询组织的一个嵌入式操作系统uTenux试用活动.本来想借助这个活动提供的开发板做一个小项目,奈何OS使用功底太次.于是,这个活动被我降低到了学习 ...

  3. git学习1:git安装和配置

    Git是什么?世界上最先进的分布式版本控制系统,记录了一个文本文件的每次一修改信息,比如,一篇散文,从草稿到最终出版,经历过无数次修改,修改了标点符号形成一个版本,老师帮助修改形成一个版本,同学帮忙修 ...

  4. [MongDB] 主从架构--官方极力不推荐

    一.缘由: 看着数据库大家庭都有主从模式,想着Mongodb应该也不会落下.但从官网看来,先是早先舍弃了Master-Master模式,现在又在不推荐 Master-Slave模式,这是要标新立异呀. ...

  5. JVM实用参数(八)GC日志

    本系列的最后一部分是有关垃圾收集(GC)日志的JVM参数.GC日志是一个很重要的工具,它准确记录了每一次的GC的执行时间和执行结果,通过分析GC日志可以优化堆设置和GC设置,或者改进应用程序的对象分配 ...

  6. 扁平设备树(FDT)

    组成 扁平设备树主要由4大部分组成:头部(header),预留内存块(memory reservation block),结构块(struct block)和字符串块(strings block).这 ...

  7. firefox火狐浏览器过滤广告插件:Adblock Plus

    firefox火狐浏览器过滤广告插件:Adblock Plus

  8. Creating a radius based VPN with support for Windows clients

    This article discusses setting up up an integrated IPSec/L2TP VPN using Radius and integrating it wi ...

  9. Android 进程通信机制之 AIDL

    什么是 AIDL AIDL 全称 Android Interface Definition Language,即 安卓接口描述语言.听起来很深奥,其实它的本质就是生成进程间通信接口的辅助工具.它的存在 ...

  10. 集成 Kendo UI for Angular 2 控件

    伴随着 Angular 2 的正式 release,Kendo UI for Angular 2 的第一批控件已经发布了,当前是 Beta 版本,免费使用. 官方站点:Kendo UI for Ang ...