POJ2743Mobile Computing[DFS 状态压缩]

时间:2022-04-30 01:58:25
Mobile Computing
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 666   Accepted: 224   Special Judge

Description

There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of them to decorate their 2-dimensional living rooms. 
In their 2-dimensional world, a mobile is defined recursively as follows: 
POJ2743Mobile Computing[DFS 状态压缩]
  • a stone hung by a string, or
  • a rod of length 1 with two sub-mobiles at both ends; the rod is hung by a string at the center of gravity of sub-mobiles. When the weights of the sub-mobiles are n and m, and their distances from the center of gravity are a and b respectively, the equation n * a = m * b holds.

For example, if you got three stones with weights 1, 1, and 2, here are some possible mobiles and their widths: 
POJ2743Mobile Computing[DFS 状态压缩] 
Given the weights of stones and the width of the room, your task is to design the widest possible mobile satisfying both of the following conditions.

  • It uses all the stones.
  • Its width is less than the width of the room.

You should ignore the widths of stones. 
POJ2743Mobile Computing[DFS 状态压缩]In some cases two sub-mobiles hung from both ends of a rod might overlap (see the figure on the right). Such mobiles are acceptable. The width of the example is (1/3) + 1 + (1/4).

Input

The first line of the input gives the number of datasets. Then the specified number of datasets follow. A dataset has the following format. 


w1 ... 
ws 
r is a decimal fraction representing the width of the room, which satisfies 0 < r < 10. s is the number of the stones. You may assume 1 <= s <= 6. wi is the weight of the i-th stone, which is an integer. You may assume 1 <= wi <= 1000. 
You can assume that no mobiles whose widths are between r - 0.00001 and r + 0.00001 can be made of given stones.

Output

For each dataset in the input, one line containing a decimal fraction should be output. The decimal fraction should give the width of the widest possible mobile as defined above. An output line should not contain extra characters such as spaces. 
In case there is no mobile which satisfies the requirement, answer -1 instead. 
The answer should not have an error greater than 0.00000001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

Sample Input

5
1.3
3
1
2
1
1.4
3
1
2
1
2.0
3
1
2
1
1.59
4
2
1
1
3
1.7143
4
1
2
3
5

Sample Output

-1
1.3333333333333335
1.6666666666666667
1.5833333333333335
1.7142857142857142

Source


题意见白书

感觉好神,我太弱了
用到了集合表示状态,每个状态保存所有二叉树的方案(开一个vector保存结构体)
dfs时枚举左右集合的元素避免重复
更新时枚举左右子树用到了哪种二叉树方案
有点类似记忆化吧,每种状态只保存了一次,只计算一次
 
代码抄的白书
//
// main.cpp
// poj2743
//
// Created by Candy on 9/28/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=;
double r,sum[<<N];
int n,w[N],T,vis[<<N];
struct node{
double l,r;
int ls,rs;
node():l(),r(){}
};
vector<node> tree[<<N];
void dfs(int subset){//printf("dfs %d\n",subset);
if(vis[subset]) return;
vis[subset]=;
int child=;
for(int left=(subset-)&subset;left;left=(left-)&subset){
child=;
int right=left^subset;
dfs(left);dfs(right); double dl=sum[right]/sum[subset],dr=sum[left]/sum[subset];
for(int i=;i<tree[left].size();i++)
for(int j=;j<tree[right].size();j++){
node t;
t.l=max(tree[left][i].l+dl,tree[right][j].l-dr);
t.r=max(tree[left][i].r-dl,tree[right][j].r+dr);
if(t.l+t.r<r) tree[subset].push_back(t);
}
}
if(!child) tree[subset].push_back(node());//leaf
}
int main(int argc, const char * argv[]) {
scanf("%d",&T);
while(T--){
scanf("%lf%d",&r,&n);
for(int i=;i<n;i++) scanf("%d",&w[i]);
for(int i=;i<(<<n);i++){
sum[i]=vis[i]=;
tree[i].clear();
for(int j=;j<n;j++) if(i&(<<j)) sum[i]+=w[j];
}
//for(int i=0;i<(1<<n);i++) printf("sum %d\n",sum[i]);
int root=(<<n)-;
dfs(root); double ans=-;
for(int i=;i<tree[root].size();i++)
ans=max(ans,tree[root][i].l+tree[root][i].r);
printf("%.10f\n",ans);
}
return ;
}

POJ2743Mobile Computing[DFS 状态压缩]的更多相关文章

  1. uva10160&lpar;dfs&plus;状态压缩&rpar;

    题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...

  2. 2101 可达性统计(拓扑排序&sol;dfs&plus;状态压缩)

    [题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...

  3. HDU 4921 Map DFS&plus;状态压缩&plus;乘法计数

    算最多十条链,能截取某前缀段,每种方案都可以算出一个权值,每种方案的概率都是总数分之一,问最后能构成的所有可能方案数. 对计数原理不太敏感,知道是DFS先把链求出来,但是想怎么统计方案的时候想了好久, ...

  4. POJ 1632 Vase collection【状态压缩&plus;搜索】

    题目传送门:http://poj.org/problem?id=1632 Vase collection Time Limit: 1000MS   Memory Limit: 10000K Total ...

  5. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  6. 最大联通子数组之和(dfs,记忆化搜索,状态压缩)

    最大联通子数组,这次的题目,我采用的方法为dfs搜索,按照已经取到的数v[][],来进行搜索过程的状态转移,每次对v[][]中标记为1的所有元素依次取其相邻的未被标记为1的元素,将其标记为1,然而,这 ...

  7. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  8. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457&amp ...

  9. hihocoder 1334 - Word Construction - &lbrack;hiho一下第170周&rsqb;&lbrack;状态压缩&plus;DFS&rsqb;

    题目链接:https://hihocoder.com/problemset/problem/1334 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given N wo ...

随机推荐

  1. Open xml 操作Excel 透视表(Pivot table)-- 实现Excel多语言报表

    我的一个ERP项目中,客户希望使用Excel Pivot table 做分析报表. ERP 从数据库中读出数据,导出到Excel中的数据源表(统一命名为Data),刷新Pivot table! 客户还 ...

  2. SDN跟网络虚拟化的完美结合

    SDN跟网络虚拟化的完美结合 之前说过,所谓的“SDN最适合的领域是数据中心”的说法,笔者认为更准确的说法应该是SDN最适合的领域是数据中心中的网络虚拟化应用.为什么说SDN 非常适合用在网络虚拟化中 ...

  3. AOP的实现机制

    1 AOP各种的实现 AOP就是面向切面编程,我们可以从几个层面来实现AOP. 在编译器修改源代码,在运行期字节码加载前修改字节码或字节码加载后动态创建代理类的字节码,以下是各种实现机制的比较. 类别 ...

  4. css的一些小技巧!页面视觉差!

    相当长的一段时间,现在网站与所谓的“视差”效果一直很受欢迎. 万一你没有听说过这种效果,不同的图像,在不同的方向移动或层主要包括.这导致了一个很好的光学效应,使参观者的注意. 在网页设计中,为了实现这 ...

  5. 电赛菜鸟营培训(零)&mdash&semi;&mdash&semi;Keil环境搭建

    一.Keil开发软件安装 1.安装keil软件 2.使用注册机进行破解 将方框内的ID号复制到注册机,然后得到License,放到最底下就可以完成了. 二.Keil工程搭建 表示参考数据手册,在这里建 ...

  6. Node&period;js 学习(三) NPM 使用介绍

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  7. Django之Model(一)--基础篇

    0.数据库配置 django默认支持sqlite,mysql, oracle,postgresql数据库.Django连接数据库默认编码使用UTF8,使用中文不需要特别设置. sqlite djang ...

  8. GitHub以及Git学习 持续编辑学习中

    官网地址: http://www.worldhello.net/gotgithub/01-explore-github/030-explore-github.html 1 加入github, http ...

  9. Javascript技巧实例精选&lpar;3&rpar;—用字符在屏幕上打印金字塔

    用Javascript实现用★字符在屏幕上打印金字塔 >>点击这里下载完整html源码<< 这是最后的截图 这是相应的Javascript源码 //动态创建表格 var s=' ...

  10. Netty 拆包粘包和服务启动流程分析

    Netty 拆包粘包和服务启动流程分析 通过本章学习,笔者希望你能掌握EventLoopGroup的工作流程,ServerBootstrap的启动流程,ChannelPipeline是如何操作管理Ch ...