UVA 624 CD (01 backpack + print path or dfs+ record path)

Source: Internet
Author: User

Description

You had a long drive by car ahead. You had a tape recorder, but the unfortunately your best music was on CDs. You need to has it on tapes so the problem to solve is:you has a tape N minutes long. How to choose tracks from CD to get the most out of tape space and has 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 was 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 is stored On the CD

InputAny 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 O Ne 3 minutes, next one 4 minutes

OutputSet of tracks (and durations) which is the correct solutions and string " sum:" and sum of duration Tim Es.

Sample Input

5 3 1 3 410 4 9 8 4 220 4 10 5 7 490 8 10 23 1 2 3 4 5 745 8 4 10 44 43 12 9 8 2

Sample Output
1 4 sum:58 2 sum:1010 5 4 sum:1910 1 2 3 4 5 7 sum:554 ten 9 8 2 sum:45

Test instructions: Give a number of M and N, and then give the number of N, to choose from the n number of several and largest numbers, and sequentially output these books and the number of the and.

DFS searches and logs the path on it. You can also use 01 backpacks.

Dfs:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 intn,m,n1,sum,ans[ A],a[ A],str[ A];6 voidDfsintXintSintp)7 {8     if(s>m)return ;9     if(s>sum)Ten     { Onesum=s; A          for(intI=0; i<p;i++) ans[i]=Str[i]; -n1=p; -     } the      for(inti=x+1; i<=n;i++) -     { -str[p]=A[i]; -DFS (i,s+a[i],p+1); +     } - } + intMain () A { at     inti; -      while(~SCANF ("%d%d",&m,&N)) -     { -sum=0; -n1=0; -          for(i=1; i<=n;i++) scanf ("%d",&a[i]); inDfs0,0,0); -          for(i=0; i<n1;i++) printf ("%d", Ans[i]); toprintf"sum:%d\n", sum); +     } -     return 0; the}

01:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 intdp[10005],v[ A][10005];6 intMain ()7 {8     intn,m,i,j;9     inta[ A];Ten      while(~SCANF ("%d%d",&m,&N)) One     { AMemset (DP,0,sizeof(DP)); -memset (V,0,sizeof(v)); -          for(i=1; i<=n;i++) scanf ("%d",&a[i]); the          for(i=n;i>=1; i--) -         { -              for(j=m;j>=a[i];j--) -             { +                 if(dp[j]<dp[j-a[i]]+A[i]) -                 { +dp[j]=dp[j-a[i]]+A[i]; Av[i][j]=1; at                 } -             } -         } -          for(i=1, j=dp[m];i<=n;i++) -         { -             if(V[i][j]) in             { -printf"%d", A[i]); toj-=A[i]; +             } -         } theprintf"sum:%d\n", Dp[m]); *     } $     return 0;Panax Notoginseng}

UVA 624 CD (01 backpack + print path or dfs+ record path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.