题意:给定完全无向图,求其中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. correlation filters in object tracking2

    http://www.cnblogs.com/hanhuili/p/4281077.html Correlation Filter in Visual Tracking系列二:Fast Visual ...

  2. 黄聪:C#中HtmlAgilityPack判断是否包含或不包含指定的属性或值

    //选择不包含class属性的节点 var result = node.SelectNodes(".//span[not(@class)]"); //选择不包含class和id属性 ...

  3. Spring+Quartz(定时任务)

    此处用到的Quartz版本是quartz-2.2.3 官方网站:http://www.opensymphony.com/quartz 首先先介绍用到的几个关键类:scheduler任务调度.Job任务 ...

  4. vim常用操作

    vim filename 编辑一个文件 在一般模式里按yy是复制的意思(复制当前行),按yy之前先按相应的数字键就是复制光标所在行到指定的行,然后按p粘贴在一般模式里按dd是删除的意思(也叫做剪切), ...

  5. 在java下使用log4j2记录日志

    1.定义: log4j2 指log4j 2.X及以上版本 2.安装 log4j-core-xx.jarlog4j-api-xx.jarlog4j-web-xx.jar(web项目的需要引用) 3.配置 ...

  6. Struts2:上传下载

    ud_upload.jsp <s:form action="fileupload" enctype="multipart/form-data"> & ...

  7. ant批量执行Jmeter脚本

    JDK,Jmeter默认已经装了 ANT下载:http://ant.apache.org/bindownload.cgi ant环境变量需要配置 ant_home,你解压之后的地址 然后PATH环境变 ...

  8. 线程Thread

    一.线程: 程序:保存在物理介质(光盘,软盘,硬盘)当中的代码片段 进程:一旦程序运行起来,就变成了操作系统当中的一个进程 线程:程序当中一条独立执行的线索 二.线程的五大状态 新生    就绪 运行 ...

  9. oracle安装操作及遇到的错误

    一.准备工作 服务器环境: 服务器端:VMWARE下的win7 64位系统 客户端:宿主机 WIN7 64位系统 软件环境: win64_Oracle_11gR2 PLSQL Developer 11 ...

  10. 【SSM 5】Mybatis分页插件的使用

    一.添加maven依赖项 <span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency&g ...