UVA 624 ---CD 01背包路径输出


DescriptionCD

You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most out of tape space and have as short unused space as possible.

Assumptions:

  • number of tracks on the CD. does not exceed 20
  • no track is longer than N minutes
  • tracks do not repeat
  • length of each track is expressed as an integer number
  • N is also integer

Program should find the set of tracks which fills the tape best and print it in the same sequence as the tracks are stored on the CD

Input

Any number of lines. Each one contains value N, (after space) number of tracks and durations of the tracks. For example from first line in sample data: N=5, number of tracks=3, first track lasts for 1 minute, second one 3 minutes, next one 4 minutes

Output

Set of tracks (and durations) which are the correct solutions and string `` sum:" and sum of duration times

Sample Input

5 3 1 3 4
10 4 9 8 4 2
20 4 10 5 7 4
90 8 10 23 1 2 3 4 5 7
45 8 4 10 44 43 12 9 8 2

Sample Output

1 4 sum:5
8 2 sum:10
10 5 4 sum:19
10 23 1 2 3 4 5 7 sum:55
4 10 12 9 8 2 sum:45

01背包输出路径;
#include<stdio.h>
#include<string.h>

#define N 1100
#define max(a,b) (a>b?a:b)

int v[N], dp[N][N], W, n;

void Path(int n, int W)
{
    if(n==0) return ;

    if(dp[n-1][W]==dp[n][W])
        Path(n-1, W);
    else
    {
        Path(n-1, W-v[n]);
        printf("%d ", v[n]);
    }
}

int main()
{
    while(scanf("%d%d", &W, &n)!=EOF)
    {
        int i, j;
        memset(v, 0, sizeof(v));
        memset(dp, 0, sizeof(dp));
        for(i=1; i<=n; i++)
            scanf("%d", &v[i]);

        for(i=1; i<=n; i++)
        for(j=0; j<=W; j++)
        {
            if(v[i]>j)
                dp[i][j] = dp[i-1][j];
            else
                dp[i][j] = max(dp[i-1][j], dp[i-1][j-v[i]]+v[i]);
        }

        Path(n, W);

        printf("sum:%d\n", dp[n][W]);
    }
    return 0;
}
View Code

一维数组:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
#define N 2510
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL;

int n, m, a[N], dp[N], path[N][N];

int main()
{
    while(scanf("%d %d", &m, &n)!=EOF)
    {
        met(dp, 0);
        met(path, 0);
        for(int i=0; i<n; i++)
            scanf("%d", &a[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=m; j>=a[i]; j--)
            {
                ///dp[j] = max(dp[j], dp[j-a[i]]+a[i]);
                if(dp[j] < dp[j-a[i]]+a[i])
                {
                    path[i][j] = 1;
                    dp[j] = dp[j-a[i]]+a[i];
                }
            }
        }
        int j = m, k = 1, ans[N] = {0};
        for(int i=n-1; i>=0; i--)
        {
            if(path[i][j])
            {
                ans[k++] = a[i];
                j -= a[i];
            }
        }
        for(int i=k-1; i>=1; i--)
            printf("%d ", ans[i]);
        printf("sum:%d\n", dp[m]);
    }
    return 0;
}
View Code

优质内容筛选与推荐>>
1、ASP添加后返回自动编号
2、Linux 下wifi 驱动开发(三)—— SDIO接口WiFi驱动浅析(转)
3、洛谷 - P2293 - 高精度开根 - 高精度
4、Session,ViewState用法
5、Nginx无需重启动态加载配置文件


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn