标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12584 Accepted Submission(s): 6379
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define INF 1 << 25 using namespace std; int w[] = {150,200,350}; int c; int d[10005], tmp[10005];//d[i]:从节点i出发到任意终点的最长路 int dp(int s) //tmp[i]保留回溯时当前对应的背包剩余空间 { int& res = d[s]; if(res != -1) return res; res = 0; for(int i = 0; i < 3 ; ++i) if(s >= w[i]) res = max(res,dp(s - w[i]) + 1); tmp[c++] = s; return res; } int main() { ios::sync_with_stdio(0); int t, n; cin >> t; while(t--) { c = 0; memset(d,-1,sizeof d); cin >> n; dp(n); int ans = INF; for(int i = 0; i < c ; ++i) ans = min(ans,tmp[i]);//从所有tmp[i]中选择最小的,即为背包问题最优时所对应的最小剩余空间 cout << ans << endl; } }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4449115.html