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:/ ...
随机推荐
- lua function
This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standar ...
- Chapter 2 - How to Add a sprite
Chapter 2 - How to Add a sprite 1. Add image resources 1.1add resources on win32 2. Add a sprite TIP ...
- Servlet中文乱码解决方法
程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件. 字节流和字符流的区别: 在Java.io包中操作文件内容的主要有两大类:字节流.字符流,两类都分为输入和输出操作. 在字节流中输 ...
- android开发之单点触摸
相对于多点触摸,单点触摸还是很简单的. 新建一个工程,先看看布局文件: <RelativeLayout xmlns:android="http://schemas.android.co ...
- iOS常见的几种延时执行的方法
1.performSelector [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay ...
- 二分图的判定hihocoder1121 and hdu3478
这两个题目都是二分图的判定,用dfs染色比较容易写. 算法流程: 选取一个没有染色的点,然后将这个点染色,那么跟他相连的所有点一定是不同颜色的,所以,如果存在已经染过颜色的,如果和这个颜色相同的话,就 ...
- sqlmap
http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注 ...
- HTTP请求方式中get和post的区别
表单提交中get和post方式的区别有5点 1.get是从服务器上获取数据,post是向服务器传送数据. 2.get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一 ...
- WPF Radio组的绑定
都是控件编,RadioButtion 简单绑定使用,model.cs下边定义属性 private int _isSuccess; public int IsSuccess { get { return ...
- PL/SQL 访问网页(get or post方式)
在我们开发plsql程序的过程中,有时候难免要访问一些外部网站的数据.这个时候我们就要用到utl_http包. 使用utl_http包前需要注意的是,当前的用户下是否有访问外部网络的权限. 如下是自己 ...