RQNOJ 57 :: 找啊找啊找GF
多维背包问题
某唐璜看中N个MM,根据性格不同一类的,需要消耗一定值的钱和人品和时间,给出某唐璜能拥有的最大钱和人品,求出如何在把到最多妹的情况下消耗最少的时间= =
好吧听起来特诡异、、
Here's my codes, 自认为比较清晰了
1 #include <stdio.h>
2
3 #define maxn 100+10
4 /*#define max(a,b) a>b?a:b*/
5
6 long ans, dp[maxn][maxn], rmb[maxn], rp[maxn], time[maxn], use_time[maxn][maxn],
7 mm_num, rmb_all, rp_all, n, best_time,
8 i, j, k, tmp;
9
10 int main(){
11 scanf ( "%ld", &n );
12 for ( i = 0; i < n; i ++ )
13 scanf ( "%ld%ld%ld", &rmb[i], &rp[i], &time[i] );
14 scanf ( "%ld%ld", &rmb_all, &rp_all );
15
16
17 for ( i = 0; i < n; i ++ )
18 for ( j = rmb_all; j >= rmb[i]; j -- )
19 for ( k = rp_all; k >= rp[i]; k -- ){
20 if ( dp[j][k] < dp[j-rmb[i]][k-rp[i]]+1 ){
21 use_time[j][k] = use_time[j-rmb[i]][k-rp[i]]+time[i];
22 dp[j][k] = dp[j-rmb[i]][k-rp[i]]+1;
23 }
24 if ( dp[j][k] == dp[j-rmb[i]][k-rp[i]]+1 &&
25 use_time[j][k] > use_time[j-rmb[i]][k-rp[i]]+time[i] ){
26 use_time[j][k] = use_time[j-rmb[i]][k-rp[i]]+time[i];
27 }
28 if ( dp[j][k] > ans )
29 ans = dp[j][k];
30 }
31
32 best_time = 1000000;
33
34 for ( j = 0; j <= rmb_all; j ++ )
35 for ( k = 0; k <= rp_all; k ++ )
36 if ( dp[j][k] == ans && use_time[j][k] < best_time )
37 best_time = use_time[j][k];
38
39 printf ( "%ld\n", best_time );
40
41 getchar(), getchar();
42 return 0;
43 }
44