码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

时间:2015-10-11 12:52:02      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

Description

技术分享
 

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

   题意:给出数m和n,再给出n个数,求从n个数中选出几个和最大的数,并按循序输出这些书和这些数的和。

   dfs搜索并记录路径就可以了。也可以用01背包。

   dfs:

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int n,m,n1,sum,ans[22],a[22],str[22];
 6 void dfs(int x,int s,int p)
 7 {
 8     if (s>m) return ;
 9     if (s>sum)
10     {
11         sum=s;
12         for (int i=0;i<p;i++) ans[i]=str[i];
13         n1=p;
14     }
15     for (int i=x+1;i<=n;i++)
16     {
17         str[p]=a[i];
18         dfs(i,s+a[i],p+1);
19     }
20 }
21 int main()
22 {
23     int i;
24     while (~scanf("%d%d",&m,&n))
25     {
26         sum=0;
27         n1=0;
28         for (i=1;i<=n;i++) scanf("%d",&a[i]);
29         dfs(0,0,0);
30         for (i=0;i<n1;i++) printf("%d ",ans[i]);
31         printf("sum:%d\n",sum);
32     }
33     return 0;
34 }

 

 01:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int dp[10005],v[22][10005];
 6 int main()
 7 {
 8     int n,m,i,j;
 9     int a[22];
10     while (~scanf("%d%d",&m,&n))
11     {
12         memset(dp,0,sizeof(dp));
13         memset(v,0,sizeof(v));
14         for (i=1;i<=n;i++) scanf("%d",&a[i]);
15         for (i=n;i>=1;i--)
16         {
17             for (j=m;j>=a[i];j--)
18             {
19                 if (dp[j]<dp[j-a[i]]+a[i])
20                 {
21                     dp[j]=dp[j-a[i]]+a[i];
22                     v[i][j]=1;
23                 }
24             }
25         }
26         for (i=1,j=dp[m];i<=n;i++)
27         {
28             if (v[i][j])
29             {
30                 printf("%d ",a[i]);
31                 j-=a[i];
32             }
33         }
34         printf("sum:%d\n",dp[m]);
35     }
36     return 0;
37 }

 

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

标签:

原文地址:http://www.cnblogs.com/pblr/p/4868972.html

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