码迷,mamicode.com
首页 > 编程语言 > 详细

POJ--3172 Scales (DFS 大容量背包 C++)

时间:2017-08-17 22:51:09      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:blog   mos   idt   while   safe   ret   sse   数组   namespace   

Scales
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3148   Accepted: 851

Description

Farmer John has a balance for weighing the cows. He also has a set of N (1 <= N <= 1000) weights with known masses (all of which fit in 31 bits) for use on one side of the balance. He places a cow on one side of the balance and then adds weights to the other side until they balance. (FJ cannot put weights on the same side of the balance as the cow, because cows tend to kick weights in his face whenever they can.) The balance has a maximum mass rating and will break if FJ uses more than a certain total mass C (1 <= C < 2^30) on one side. 

The weights have the curious property that when lined up from smallest to biggest, each weight (from the third one on) has at least as much mass as the previous two combined. 

FJ wants to determine the maximum mass that he can use his weights to measure exactly. Since the total mass must be no larger than C, he might not be able to put all the weights onto the scale. 

Write a program that, given a list of weights and the maximum mass the balance can take, will determine the maximum legal mass that he can weigh exactly.

Input

Line 1: Two space-separated positive integers, N and C. 

Lines 2..N+1: Each line contains a single positive integer that is the mass of one weight. The masses are guaranteed to be in non-decreasing order.

Output

Line 1: A single integer that is the largest mass that can be accurately and safely measured.

Sample Input

3 15
1
10
20

Sample Output

11

Hint

Explanation of the sample: 

FJ has 3 weights, with masses of 1, 10, and 20 units. He can put at most 15 units on one side of his balance. 

The 1 and 10 weights are used to measure 11. Any greater weight that can be formed from the weights will break the balance.
 
 
 
这道题主要是数据巨大,数组开不开原先01套路不能用!
巨大的剪枝DFS
留做参考,立个以后不错这个题的flag;
#include<iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
int a[1005];
ll sum[1005];
ll ans,m,n;
void dfs(ll x, ll cnt)
{
    if(cnt>ans)
        ans=cnt;
    if(x<1)
        return ;
    for(int i =x;i>=1;i--)
    {
        if(cnt+sum[i]<=ans)
            continue;
        if(cnt+a[i]>m)
            continue;
        dfs(i-1,cnt+a[i]);
    }
}
int main()
{

    while(~scanf("%lld%lld",&n,&m))
    {
        ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        dfs(n,0);
        printf("%lld",ans);
    }
}

  

POJ--3172 Scales (DFS 大容量背包 C++)

标签:blog   mos   idt   while   safe   ret   sse   数组   namespace   

原文地址:http://www.cnblogs.com/ygtzds/p/7384612.html

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