POJ2743Mobile Computing[DFS 状态压缩]

本文探讨了一个在二维空间中利用不同重量的石头构造最宽移动装饰的艺术问题。通过使用集合表示状态的方法,并借助二叉树结构存储解决方案,文章提供了一种高效的递归算法来寻找满足特定宽度限制的最佳配置。
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: 
  • 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: 
 
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. 
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 ... 
w s 
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=6;
double r,sum[1<<N];
int n,w[N],T,vis[1<<N];
struct node{
    double l,r;
    int ls,rs;
    node():l(0),r(0){}
};
vector<node> tree[1<<N];
void dfs(int subset){//printf("dfs %d\n",subset);
    if(vis[subset]) return;
    vis[subset]=1;
    int child=0;
    for(int left=(subset-1)&subset;left;left=(left-1)&subset){
        child=1;
        int right=left^subset;
        dfs(left);dfs(right);
        
        double dl=sum[right]/sum[subset],dr=sum[left]/sum[subset];
        for(int i=0;i<tree[left].size();i++)
            for(int j=0;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=0;i<n;i++) scanf("%d",&w[i]);
        for(int i=0;i<(1<<n);i++){
            sum[i]=vis[i]=0;
            tree[i].clear();
            for(int j=0;j<n;j++) if(i&(1<<j)) sum[i]+=w[j];
        }
        //for(int i=0;i<(1<<n);i++) printf("sum %d\n",sum[i]);
        int root=(1<<n)-1;
        dfs(root);
        
        double ans=-1;
        for(int i=0;i<tree[root].size();i++)
            ans=max(ans,tree[root][i].l+tree[root][i].r);
        printf("%.10f\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/candy99/p/5918400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值