uva 624
背包問題 ?總時間為容量,單個唱片時間為各個物體的價值與體積 ? f【】 用來記錄路徑
#include <cstdio> #include <cstring> #define M 10005int a[25],d[M],f[25][M]; int main() {int V;while(scanf("%d",&V) == 1){int n;memset(d,0,sizeof(d));memset(f,0,sizeof(f));scanf("%d",&n);for(int i = 0; i < n; i++)scanf("%d",&a[i]);for(int i = 0; i < n; i++){for(int j = V; j >= a[i]; j--){if(d[j] <= d[j-a[i]]+a[i]){d[j] = d[j-a[i]]+a[i];f[i][j] = 1;}}}for(int i = n-1, j = V; i >= 0; i--){if(f[i][j]){printf("%d ",a[i]);j -= a[i];}}printf("sum:%d\n",d[V]);}return 0; }轉載于:https://www.cnblogs.com/avema/p/3774251.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: 转:给自己TopCoder SRM的建议
- 下一篇: 分享codeigniter框架,在zen