{POJ}{3925}{Minimal Ratio Tree}{最小生成树}
题意:给定完全无向图,求其中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}{最小生成树}的更多相关文章
- HDU 2489 Minimal Ratio Tree 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)
Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- 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 ...
- HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
- hdu2489 Minimal Ratio Tree
hdu2489 Minimal Ratio Tree 题意:一个 至多 n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...
- HDUOJ----2489 Minimal Ratio Tree
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Minimal Ratio Tree HDU - 2489
Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...
- HDU 2489 Minimal Ratio Tree(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- LVM在线扩容
我虚拟机根分区已经使用了35%,现在需要对他进行在线扩容,扩容之后使用率降到30% [root@localhost ~]# dfFilesystem 1K-blocks Used Available ...
- IntentService
http://developer.android.com/training/run-background-service/index.html IntentService 只是简单的对Service做 ...
- 代码安装apk文件
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); in ...
- 常用Java排序算法
常用Java排序算法 冒泡排序 .选择排序.快速排序 package com.javaee.corejava; public class DataSort { public DataSort() { ...
- codeforces 83 D. Numbers
题意: 给出l,r,k,(1 ≤ l ≤ r ≤ 2·109, 2 ≤ k ≤ 2·109) 求在区间[l,r]内有多少个数i满足 k | i,且[2,k-1]的所有数都不可以被i整除 首先,如果k不 ...
- Android 对话框(Dialog)大全 建立你自己的对话框
Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...
- AS快捷键
Ctrl+Q 显示关键字的提示文档 Ctrl+鼠标点击 查看关键字的源码 ctrl+T 在不同的选项卡中进行切换 ctrl+J 提示当前位置输入模板 Ctrl+P 提示参数 Alt+Enter 提示 ...
- Oracle定时查询结果输出到指定的log文件
最近有个监控项目需要采集数据库信息,原来方案是写个sql脚本,每个脚本放一个查询语句然后通过操作系统层su到oracle用户通过sqlpus执行这个.sql,然后加到crontab定时执行.但是这个问 ...
- 从高处理解android与服务器交互(看懂了做开发就会非常的容易)
今天帮一个朋友改一个bug 他可以算是初学者吧 .我给他看了看代码,从代码和跟他聊天能明显的发现他对客户端与服务器交互 基本 不是很了解.所以我花了更多时间去给他讲客户端与服务器的关系.我觉得从这个高 ...
- 对想进入Unity开发新人的一些建议
提前声明:本文只是写给那些非职业游戏开发人士,只面向那些在校本科生,或已就业但无unity背景的同学们,当然是面对程序员方向的.本人刚工作也没多久,资历尚浅,之前在网上有一位同学让我谈谈一些想法,所以 ...