UVA 624 —— 01背包&路径输出
2013-11-04 22:06
211 查看
624 - CD
Time limit: 3.000 secondsCD |
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) numberof 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 durationtimes.
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; }
相关文章推荐
- UVA 题目624 CD(01背包输出路径啊)
- UVA 624 ---CD 01背包路径输出
- UVa 624 CD (01背包路径输出)
- uva624 CD (01背包+路径的输出)
- CD - UVa 624 01背包记录路径
- uva 624 CD (01背包+打印路径)
- UVa 11419 我是SAM(最小点覆盖+路径输出)
- UVA 624 (0 1背包 + 打印路径)
- uva 116 Unidirectional TSP 单向TSP 问题,经典dP(路径输出注意规划方向)
- uva624 CD(dp 0-1 背包 打印路径)
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
- UVA 10537 The Toll! Revisited(dijkstra输出字典序最小的路径)
- UVALive 3713 Astronauts(2-sat+输出任意路径)
- 1、UVA624(记录路径问题)
- UVA 624 CD(01背包输出)
- (UVA - 208)Firetruck(路径输出问题,回溯+并查集/floyd算法+dfs)
- UVA 624 CD(打印路径的01背包)
- UVA 624 CD(01背包/记录路径)
- UVa 872 Ordering(拓扑排序输出全部路径)
- UVA 624 CD(01背包,要记录路径)