uva 1354 Mobile Computing

题目:Mobile Computing


题意:有s块石头,用一些长为1的木棍挂着,满足杠杆平衡条件。问在房间里最宽能挂下多宽。


思路:从下向上构造二叉树,每次从已知集合中随机抽取两个杠杆(或石头)组合起来加入集合。计算时用bfs。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<sstream>
using namespace std;

struct Node{
	int id;
	int w;
	int x,y;
	Node(){}
	Node(int one,double two,double three,int four){
		w=one,x=two,y=three,id=four;
	}
	bool operator < (const Node& other) const {
		if(w<other.w) return true;
		if(w>other.w) return false;
		if(x<other.x) return true;
		if(x>other.x) return false;
		if(y<other.y) return true;
		return false;
	}
};

int n;
double Droom;
double minL=10,maxR=-10;

void bfs(Node a,vector<Node> vec){
	double p[100]={0};
	vector<Node> que;
	que.push_back(a);
	int t=0;
	while(t<que.size()){
		Node head=que[t];
		t++;
		if(head.x!=-1&&head.y!=-1){
			que.push_back(vec[head.x]);
			que.push_back(vec[head.y]);
			p[head.x]=p[head.id]-(double)vec[head.y].w/(vec[head.y].w+vec[head.x].w);
			p[head.y]=p[head.id]+(double)vec[head.x].w/(vec[head.y].w+vec[head.x].w);
		}
	}
	double L=10,R=-10;
	for(int i=0;i<vec.size();i++){
		L=min(L,(double)p[i]);
		R=max(R,(double)p[i]);
	}
	if(R-L>Droom||R-L<maxR-minL) return ;
	maxR=R,minL=L;
}

void dfs(vector<Node> a,vector<Node> vec){
	if(a.size()==1){
		bfs(a[0],vec);
		return ;
	}
	for(int i=0;i<a.size();i++){
		for(int j=0;j<a.size();j++){
			if(i==j) continue;
			vector<Node> x,y=vec;
			map<Node,int> mp2;
			Node left=a[i],right=a[j];
			Node add=Node(left.w+right.w,a[i].id,a[j].id,y.size());
			y.push_back(add);
			for(int k=0;k<a.size();k++){
				if(k!=i&&k!=j){
					x.push_back(a[k]);
				}
			}
			x.push_back(add);
			dfs(x,y);
		}
	}
	return ;
}

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		minL=10,maxR=-10;
		scanf("%lf%d",&Droom,&n);
		vector<Node> a,vec;
		for(int i=0;i<n;i++){
			int x;
			scanf("%d",&x);
			a.push_back(Node(x,-1,-1,i));
			vec.push_back(Node(x,-1,-1,i));
		}
		dfs(a,vec);
		if(minL!=10&&maxR!=-10) printf("%.16lf\n",maxR-minL);
		else printf("-1\n");
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值