您的位置:首页 > 其它

UVA 624 —— 01背包&路径输出

2013-11-04 22:06 211 查看


624 - CD

Time limit: 3.000 seconds


  CD 
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


Miguel A. Revilla 

2000-01-10
题意是n个商品,给你他的价值,问你价值和不超过N且最大的价值和是多少,输出最优解中各物品价值。
思路是一个价值和花费一样大的01背包,然后用path记录路径。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))

#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 10000 + 5;
const int maxw = 100 + 20;
const int MAXNNODE = 1000000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
//#pragma comment(linker, "/STACK:102400000,102400000")
int dp[MAXN][MAXN];
int cost[MAXN] , value[MAXN] , N;
int path[MAXN][MAXN];
int sum[MAXN];
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
while(~scanf("%d" , &N))
{
int n;
scanf("%d" , &n);
for(int i = 1 ;i <= n ; i++)
{
scanf("%d", &value[i]);
cost[i] = value[i];
}
for(int i = 0 ; i <= n ; i++)
{
for(int j = 0 ;  j <= N ; j++)
{
dp[i][j] = 0 , path[i][j] = 0;
}
}
for(int i = 1 ;i <= n ; i++)
{
for(int j = 0 ;j <= N ;j++)
{
dp[i][j] = dp[i - 1][j];
if(j >= cost[i])
{
if(dp[i][j] < dp[i - 1][j - cost[i]] + value[i])
{
path[i][j] = 1;
dp[i][j] = dp[i - 1][j - cost[i]] + value[i];
}
}

}
}
int j = N;
for(int i = n ; i >= 0 ; i--)
{
if(path[i][j] == 1 && j >= 0)
{
printf("%d " , cost[i]);
j -= cost[i];
}
}
printf("sum:%d\n", dp

);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM DP