标签:des style blog class code color
Time Limit: 1000MS | Memory Limit: 65535KB |
Submissions: 380 | Accepted: 113 |
70 3 71 100 69 1 1 2
3
方法一
DFS 超时
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 |
#include <iostream> using
namespace std; int
value,num; int
val[100],tim[100]; int
MAX_Val; int
flag,temp; int
vis[100]; void
dfs( int
n) { if (n>num) return
; if (temp>value) return
; if (n==num&&MAX_Val<flag) { MAX_Val = flag; } for ( int
i=0;i<num;i++) { if (!vis[i]) { temp+=tim[i]; flag+=val[i]; vis[i] = 1; dfs(i+1); vis[i] = 0; temp-=tim[i]; flag-=val[i]; } } } int
main() { cin>>value>>num; for ( int
i=0;i<num;i++) { cin>>tim[i]>>val[i]; } MAX_Val = 0; flag =0;temp=0; dfs(0); cout<<MAX_Val<<endl; return
0; } |
方法二
DP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
#include<iostream> # include<cstring> # define max(a,b) a>b?a:b using
namespace std; int
main() { int
dp[101][1001],m,T,w[101],val[101],i,j; cin>>T>>m; for (i=1;i<=m;i++) cin>>w[i]>>val[i]; memset (dp,0, sizeof (dp)); for (i=1;i<=m;i++) for (j=0;j<=T;j++) //j相当于上面说的V-c[i] { if (j>=w[i]) dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+val[i]); //放还是不放的选择 else
dp[i][j]=dp[i-1][j]; } cout<<dp[m][T]<<endl; return
0; } |
标签:des style blog class code color
原文地址:http://www.cnblogs.com/locojyw/p/3705173.html