hdu 2489 Minimal Ratio Tree
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的更多相关文章
- HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
- 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 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (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 ...
- HDU 2489 Minimal Ratio Tree(prim+DFS)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- Minimal Ratio Tree HDU - 2489
Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...
- 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 ...
随机推荐
- 重启电脑提示Error:no such partition grub rescue
我的系统是Win7,在使用Ubuntu12.04自带的Wubi.exe安装双系统时,系统提示重新启动计算机,重启后电脑就停留在了黑屏界面并提示: error:no such partition gru ...
- DDP和DDU什么区别
DU/DDP 就是A发货给国外B,B只要呆在家里看电视,货会自动送上门,当中的所有运输清关等事情都是由A来负责(A可以委托货代来负责),区别就是DDU是不包括税金的,也就是货值的百分之多少,税金会在B ...
- Spring MVC Controller 单元测试
简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- 选择排序(Selection Sort)
选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...
- 【HDU1233】还是畅通工程(MST基础题)
无坑,裸题.直接敲就恩那个AC. #include <iostream> #include <cstring> #include <cstdio> #include ...
- hdu 4400 Mines(离散化+bfs+枚举)
Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all ...
- 用WIFI为什么连不上VPN
因为是做服务器的,经常需要通过VPN连接到公司的服务器处理一些事件. 但最近一次在寝室通过WIFI连接VPN时却报“错误 619:不能建立到远程计算机的连接,因用于此端口的连接已关闭”.我的第一反应是 ...
- wpf纯前台绑定
<Window x:Class="Example1.MainWindow" ... xmlns:local="clr-namespace:Example1" ...
- JQuery Ajax 获取数据
前台页面: 对一张进行查询,删除,添加 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"& ...