UVa 990 - Diving for Gold

原创 2015年11月18日 10:08:48

題目:一個潛水員冒險家,在水下發現了一個沉船,他只有一個氧氣瓶,可以提供t時間的氧氣;

            沉船裡面有一下寶箱,他們的價值和深度已知,取得每個寶物的代價是3*w*d(i),求最大價值。

分析:dp,01背包。數據較少,直接利用位狀態記錄每個元素的選取情況。

說明:直接將t/3w,可以調高計算效率。

#include <cstring>
#include <cstdio>

int v[30], d[30], f[3334], s[3334];

int main()
{
	int t, w, n, cases = 0;
	while (~scanf("%d%d%d",&t,&w,&n)) {
		for (int i = 0; i < n; ++ i)
			scanf("%d%d",&d[i],&v[i]);
		
		t /= 3*w;
		memset(f, 0, sizeof(f));
		memset(s, 0, sizeof(s));
		for (int i = 0; i < n; ++ i)
			for (int j = t; j >= d[i]; -- j)
				if (f[j] < f[j-d[i]]+v[i]) {
					f[j] = f[j-d[i]]+v[i];
					s[j] = s[j-d[i]]|(1<<i);
				}
		
		int sum = 0;
		for (int i = 0; i < 30; ++ i)
			if(s[t]&(1<<i))
				sum ++;
			
		if (cases ++) printf("\n");
		printf("%d\n%d\n",f[t],sum);
		for (int i = 0; i < 30; ++ i) 
			if (s[t]&(1<<i))
				printf("%d %d\n",d[i],v[i]);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

UVa 990 - Diving for Gold DP 0-1

Diving for gold Problem John is a diver and a treasure hunter. He has just found the location of a...

QQ2007 for P990

  • 2009-08-04 01:29
  • 440KB
  • 下载

Art_of_Programming_SE_for_uva

  • 2010-03-05 14:00
  • 1.78MB
  • 下载

Rsorder 6% off: Paypal no confirmation for gold runescape with the code RSOPAYPAL

cheapest runescape gold with paypal no confirmation now! 6% discount code at Rsorder.com for paypal ...

RSorder offer Xmas Gift Packs for buying runescape 3 gold

A pretty good site to buy rs gold on RSorder offering fast,efficient service and Lowest Price.Doin...

Free FIFA 15 ps4 ultimate team coins Giveaway for Everyone Liking Gold4fans Facebook

What Ruben seems 100% Winning Rate: Get Free FIFA 15 coins by clicking "Like" on Gold4fans faceb...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)