link:http://acm.hdu.edu.cn/showproblem.php?pid=2191
最简单的那种
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <queue> 9 #include <deque> 10 #include <queue> 11 #include <list> 12 #include <map> 13 #include <set> 14 #include <vector> 15 #include <utility> 16 #include <functional> 17 #include <fstream> 18 #include <iomanip> 19 #include <sstream> 20 #include <numeric> 21 #include <cassert> 22 #include <ctime> 23 #include <iterator> 24 const int INF = 0x3f3f3f3f; 25 const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; 26 using namespace std; 27 int n, V, t, f[111], c[111], w[111], m[111]; 28 void zeropack(int c, int w) 29 { 30 for (int v=V; v>=c; --v) 31 { 32 f[v]=max(f[v],f[v-c]+w); 33 } 34 return; 35 } 36 void completepack(int c, int w) 37 { 38 for (int v=c;v<=V;++v) 39 f[v]=max(f[v],f[v-c]+w); 40 return; 41 } 42 void multipack(int c, int w, int m) 43 { 44 if (c*m >= V) 45 { 46 completepack(c,w); return; 47 } 48 int k=1; 49 while (k<m) 50 { 51 zeropack(c*k,w*k); m-=k; k*=2; 52 } 53 zeropack(c*m,w*m); 54 return; 55 } 56 int main(void) 57 { 58 ios::sync_with_stdio(false); 59 #ifndef ONLINE_JUDGE 60 freopen("in.txt", "r", stdin); 61 #endif // ONLINE_JUDGE 62 int t; cin>>t; 63 while (t--) 64 { 65 cin>>V>>n; 66 for (int i=0;i<n;++i) cin>>c[i]>>w[i]>>m[i]; 67 memset(f,0,sizeof(f)); 68 for (int i=0;i<n;++i) 69 { 70 multipack(c[i],w[i],m[i]); 71 } 72 cout<<f[V]<<endl; 73 } 74 return 0; 75 }
其实这种动态规划题目还是要注意初始化,想想问什么这样初始化。