poj2923 Relocation

Description

Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything a bit. Since the
furniture does not fit into the cars, Eric wants to put them on top of the cars. However, both cars only support a certain weight on their roof, so they will have to do several trips to transport everything. The schedule for the move is planed like this:

  1. At their old place, they will put furniture on both cars.
  2. Then, they will drive to their new place with the two cars and carry the furniture upstairs.
  3. Finally, everybody will return to their old place and the process continues until everything is moved to the new place.

Note, that the group is always staying together so that they can have more fun and nobody feels lonely. Since the distance between the houses is quite large, Eric wants to make as few trips as possible.

Given the weights wi of each individual piece of furniture and the capacities C1 and C2 of the two cars, how many trips to the new house does the party have to make to move all the furniture? If
a car has capacity C, the sum of the weights of all the furniture it loads for one trip can be at most C.

Input

The first line contains the number of scenarios. Each scenario consists of one line containing three numbers nC1 and C2C1 and C2 are the capacities of the cars (1
≤ Ci ≤ 100) and n is the number of pieces of furniture (1 ≤ n ≤ 10). The following line will contain n integers w1, …, wn, the weights of the furniture (1 ≤ wi ≤
100). It is guaranteed that each piece of furniture can be loaded by at least one of the two cars.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line with the number of trips to the new house they have to make to move
all the furniture. Terminate each scenario with a blank line.

Sample Input

2
6 12 13
3 9 13 3 10 11
7 1 100
1 2 33 50 50 67 98

Sample Output

Scenario #1:
2

Scenario #2:

3

这道题是用状态压缩+01背包,因为物品只有10件,所以会想到状压dp,先把所有的物品都用0和1表示,0表示物品还没有被搬走,1表示已经被搬走,用dp[state]记录从所有物品都未被搬走到状态state所要搬的最少次数。可以先算出哪些状态是一次就能搬走的,这里要用到01背包的思想,先看看该状态下c1可以容纳多少重量,然后用看看把这些重量搬走后剩下的质量能不能被c2所容纳。这些都处理完后,就用状态转移方程dp[j|state]=min(dp[j|state,dp[state]+1},这个方程很巧妙啊,开始还以为是两个能一次搬走的状态结合,其实是利用能一次搬走,所以在之前的基础上加上这个一次能搬走的状态。最后的结果就是dp[1<<(n-1)]啦。
#include<stdio.h>
#include<string.h>
#define inf 88888888
int c1,c2,n,w[15],state,yici[1500],vis[150],dp[1500];
int min(int x,int y){
	return x<y?x:y;
}
int panduan(int x)
{
	int i,j,sum=0;
	memset(vis,0,sizeof(vis));
	vis[0]=1;
	for(i=1;i<=n;i++){
		if((1<<(i-1))&x){
			 sum+=w[i];
		     for(j=c1;j>=w[i];j--){
			    if(vis[j-w[i]]){
			      vis[j]++;
			    }
		     }
		}
	}
	if(sum>c1+c2)return 0;
	for(i=0;i<=c1;i++){
		if(vis[i] && sum-i<=c2)return 1;
	}
	return 0;
}

int main()
{
	int m,i,j,T,t,num1=0;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&c1,&c2);
		for(i=1;i<=n;i++){
			scanf("%d",&w[i]);
		}
		t=0;
		memset(yici,0,sizeof(yici));
		for(i=1;i<(1<<n);i++){
			dp[i]=inf;
			if(panduan(i)){
				yici[++t]=i;
				dp[i]=1;
			}
		}
		dp[0]=0;
		for(i=1;i<(1<<n);i++){
			for(j=1;j<=t;j++){
				if(yici[j]&i)continue;
				dp[i|yici[j]]=min(dp[i|yici[j]],dp[i]+1);
			}
		}
		num1++;
		printf("Scenario #%d:\n",num1);
		printf("%d\n\n",dp[(1<<n)-1]);
	}
	return 0;
}

时间: 2024-10-09 17:46:36

poj2923 Relocation的相关文章

POJ2923:Relocation(状态压缩+01)

Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything

POJ2923——背包DP(01+状压)——Relocation

Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything

linux 生成动态库时提示relocation R_X86_64_32 against `.rodata&#39; can not be used when making a shared object;

linux生成动态库时遇到了relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC错误. 由于我的系统是AMD64位的,所以需要在编译的时候添加 -fPIC选项 解决方法: 例如: g++ -c -fPIC head.cpp    生成head.o g++ -fpic -shared -o libfun.so head.o linux 生成

Linux ELF格式文件解析之relocation和entry入口点,基于gas汇编语言视角

问题引入: Linux的elf文件一开始理解起来的确很难,有的人可能会去看<linkers and loaders>,这书的确好,但是没有详细的解释很多细节,尤其是从汇编语言视角.我读了这本书很多地方后还是不明白,然后我又读了IBM360计算机的汇编器设计文档,里面详解了二次扫描汇编器的设计原理,以及relocation概念.但这些依然解决不了我的疑惑,因为困扰我的是一个选项,即ld -Ttext=org,我不明白这个org偏移会对程序产生什么影响,一开始我以为这选项只不过更改了elf的文件头

poj 2923 Relocation 解题报告

题目链接:http://poj.org/problem?id=2923 题目意思:给出两部卡车能装的最大容量,还有n件物品的分别的weight.问以最优方式装入,最少能运送的次数是多少. 二进制表示物品状态:0表示没运走,1表示已被运走. 枚举出两辆车一趟可以运出的状态.由于物品是一趟一趟运出来的.所以就可以由一个状态通过两辆车一趟的状态转移到另一个状态. dp[i]=MIN(dp[k]+1).k可以由两车一趟转移到i. 我是参考此人的:http://blog.csdn.net/bossup/a

[uboot] (番外篇)uboot relocation介绍

http://blog.csdn.net/ooonebook/article/details/53047992 以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为例 [uboot] uboot流程系列: [project X] tiny210(s5pv210)上电启动流程(BL0-BL2) [uboot] (第一章)uboot流程——概述 [uboot] (第二章)uboot流程——uboot-spl编译流程 ========================

uboot的relocation原理详细分析

最近在一直在做uboot的移植工作,uboot中有很多值得学习的东西,之前总结过uboot的启动流程,但uboot一个非常核心的功能没有仔细研究,就是uboot的relocation功能. 这几天研究下uboot的relocation功能,记录在此,跟大家共享. 所谓的relocation,就是重定位,uboot运行后会将自身代码拷贝到sdram的另一个位置继续运行,这个在uboot启动流程分析中说过. 但基于以前的理解,一个完整可运行的bin文件,link时指定的链接地址,load时的加载地址

POJ 2923 Relocation(状压+背包)

Relocation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2340   Accepted: 965 Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them r

arm平台编译动态库报 relocation R_ARM_MOVW_ABS_NC 错误解决

编译一个能在 arm 平台上用的动态库,结果报错如下: relocation R_ARM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC ./obj/xxx.o: could not read symbols: Bad value collect2: error: ld returned 1 exit status 其实错误信息已经给出解决