Minimal Ratio Tree
hdu2489:http://acm.hdu.edu.cn/showproblem.php?pid=2489
题意:给你一个n个节点图,图的点有边权和点权,然后选取m个节点的子图,然后求这个一棵树,然后让这棵树的所有边权之和/所有点权之和的值最少。
题解:n很小,只有15,所以可以直接暴力,枚举每一种情况,然后求每一种情况的最小生成树,取最小的就可以了。但是这里的学到一个新的东西。就是判断a<b的时候,有可能a==b的时候由于浮点误差,也会被判成a<b,但是题目要的是字典序最小的,所以相等的时候是不能取的,所以要先考虑a和b是否相等,在不等的情况下在进行a<b的判断。
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int fa[],node[],ans[],val[],g[][];
int n,m;
double last;
void init(){
for(int i=;i<=;i++)
fa[i]=i;
}
int Find(int x){
int s;
for(s=x;s!=fa[s];s=fa[s]);
while(x!=s){
int temp=fa[x];
fa[x]=s;
x=temp;
}
return s;
}
struct Node{
int x,y;
int w;
bool operator<(const Node a)const{
return w<a.w;
}
}num[];
void solve(int *a){
int top=,cost=;
for(int i=;i<=n;i++)
if(a[i])cost+=val[i]; for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(a[i]&&a[j]&&g[i][j]){
num[++top].x=i;
num[top].y=j;
num[top].w=g[i][j];
}
sort(num+,num+top+);
int ct1=,ct2=;
init();
for(int i=;i<=top;i++){
int tx=Find(num[i].x);
int ty=Find(num[i].y);
if(tx==ty)continue;
else{
fa[tx]=ty;
ct1+=num[i].w;
ct2++;
if(ct2==m-)break;
}
}
if(ct2==m-){
double temp=(double)ct1/(double)cost;
if(abs(temp-last)>0.00000001&&temp<last){
last=temp;
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++)
if(a[i])
ans[i]=i;
}
}
}
void DFS(int depth,int start){
if(depth==m+){
solve(node);
return;
}
for(int i=start;i<=n;i++){
node[i]=;
DFS(depth+,i+);
node[i]=;
}
}
int main(){
while(~scanf("%d%d",&n,&m)&&n){
memset(g,,sizeof(g));
memset(ans,,sizeof(ans));
memset(node,,sizeof(node));
for(int i=;i<=n;i++)
scanf("%d",&val[i]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&g[i][j]);
last=100000000.0;
DFS(,);
int aa=;
for(int i=;i<=n;i++)
if(ans[i]){
if(!aa)printf("%d",ans[i]);
else
printf(" %d",ans[i]);
aa=;
}
puts("");
}
}
Minimal Ratio Tree的更多相关文章
- hdu2489 Minimal Ratio Tree
hdu2489 Minimal Ratio Tree 题意:一个 至多 n=15 的 完全图 ,求 含有 m 个节点的树 使 边权和 除 点权和 最小 题解:枚举 m 个 点 ,然后 求 最小生成树 ...
- 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 ...
- HDUOJ----2489 Minimal Ratio Tree
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 ...
- HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】
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 (dfs+Prim最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and ...
- 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:/ ...
随机推荐
- 自主创建tcpdump/wireshark pcap文件
pcap文件格式是bpf保存原始数据包的格式,很多软件都在使用,比如tcpdump.wireshark等等,了解pcap格式可以加深对原始数据包的了解,自己也可以手工构造任意的数据包进行测试. p ...
- hdu 1199 Color the Ball(离散化线段树)
Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 项目打包 tomcat部署
IDE: IDEA 1.项目maven管理先执行 clean,再执行 compile 2.如果编译compile不成功,则将 C:\Users\Administrator\.m2\repository ...
- sendkeys && appactivate
sendkeys 用于输入键盘按键 appactivate 用于聚焦程序 on error resume next set ws = createObject("wscript.she ...
- Asp.net简单三层+Sqllite 增删改查
新建项目à新建一个空白解决方案 在Model新建一个实体类 using System; using System.Collections.Generic; using System.Linq; usi ...
- 【转】Angular Input格式化
今天在Angular中文群有位同学问到:如何实现对input box的格式化.如下的方式对吗? <input type="text" ng-model="demo. ...
- jquery ajax跨域取数据
jsonp.js/html 主要是利用jquery ajax和jsonp的datatype 跨站点请求数据,记录~ 同源策略:同端口,同协议,同域:所以ajax不能支持跨域取得数据,解决方案一般是js ...
- Error Domain=com.google.greenhouse Code=-102
*** Terminating app due to uncaught exception 'com.google.greenhouse', reason: 'Error Domain=com.goo ...
- C++判断Office版本——转载
自:http://blog.csdn.net/lpc_china/article/details/18359145 主要原理:查询windows注册表microsoft office软件项的值来判断版 ...
- 原始的JDBC操作
-----------------------------根据配置文件---------------------------- package cn.gdpe.jdbc; import java.io ...