现在的位置: 首页 > 综合 > 正文

装载问题

2015年12月27日 ⁄ 综合 ⁄ 共 575字 ⁄ 字号 评论关闭
文章目录

Problem Description

在一批共n个集装箱要装上艘载重量为c的轮船,其中集装箱i的重量为wi。找出一种最优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱装上轮船。

Input

输入有多组数据,每组数据的第一行有两个正整数n和c。n是集装箱数,c是轮船的载重量。接下来的1行中有n个正整数,表示集装箱的重量。

Output

对于每组数据输出最大装载重量。

Sample Input

5 10
7 2 6 5 4

Sample Output

10


<pre name="code" class="cpp">#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int c , n , ans;
int a[1000];
bool f[10000];

int main()
{
    while (cin >> n >> c)
    {
        for(int i = 1 ; i <= n ; i++)    scanf("%d" , &a[i]);
        ans = 0;
        memset(f,0,sizeof(f));
        f[0] = true;
        for(int i = 1 ; i <= n ; i++)
            for(int j = c ; j >= a[i] ; j--)
                if (f[j - a[i]] == true)
                {
                    f[j] = true;
                    if (j > ans) ans = j;
                }
        cout << ans << endl;
    }
    return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.