码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2743Mobile Computing[DFS 状态压缩]

时间:2016-09-29 01:15:05      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

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 ... 
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=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;
}

 

POJ2743Mobile Computing[DFS 状态压缩]

标签:

原文地址:http://www.cnblogs.com/candy99/p/5918400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!