hdu-2602&&POJ-3624---01背包裸题
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2602
https://vjudge.net/problem/POJ-3624
都是01背包的裸题
这里有01背包训练集,欢迎大佬来AK
hdu2602:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int T, n, m; 7 const int maxn = 1000 + 10; 8 int dp[maxn], v[maxn], w[maxn]; 9 int main() 10 { 11 cin >> T; 12 while(T--) 13 { 14 cin >> n >> m; 15 memset(dp, 0, sizeof(dp)); 16 for(int i = 0; i < n; i++)cin >> v[i]; 17 for(int i = 0; i < n; i++)cin >> w[i]; 18 for(int i = 0; i < n; i++) 19 { 20 for(int j = m; j >= w[i]; j--) 21 { 22 dp[j] = max(dp[j], dp[j - w[i]] + v[i]); 23 } 24 } 25 cout<<dp[m]<<endl; 26 } 27 return 0; 28 }
POJ-3624
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int T, n, m; 7 const int maxn = 100000 + 10; 8 int dp[maxn], v[maxn], w[maxn]; 9 int main() 10 { 11 //cin >> T; 12 { 13 cin >> n >> m; 14 memset(dp, 0, sizeof(dp)); 15 for(int i = 0; i < n; i++)cin >> w[i] >> v[i]; 16 for(int i = 0; i < n; i++) 17 { 18 for(int j = m; j >= w[i]; j--) 19 { 20 dp[j] = max(dp[j], dp[j - w[i]] + v[i]); 21 } 22 } 23 cout<<dp[m]<<endl; 24 } 25 return 0; 26 }
越努力,越幸运