zoukankan      html  css  js  c++  java
  • UVA 624 ---CD 01背包路径输出

    DescriptionCD

    Download as PDFYou 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
    ", 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
    ", dp[m]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    python float保留两位小数
    linux使用ftp服务
    selenium是如何启动浏览器的
    小白学PYTHON时最容易犯的6个错误,看看你遇到过几个
    为什么你需要测试某个字段为空的场景
    为什么要做接口测试
    从爱奇艺招聘信息看当前测试开发的技能要求
    测试同学难道要写一辈子的hello world?
    收藏清单: python测试框架最全资源汇总
    如何写出测不出bug的测试用例
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4731931.html
Copyright © 2011-2022 走看看