nyoj311 完全背包

完全背包问题

1、用max()函数,结果TLE了

2、果然scanf和printf比cin,cout快多了

#include <cstdio>
#include <cstring>
int f[50001];
int c[2001],w[2001];
void completePack(int V,int i){
    for(int v = c[i];v <= V;v++){
        f[v] = f[v]>f[v - c[i]] + w[i]?f[v]:f[v-c[i]]+w[i];
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    int N,m,V;
    scanf("%d",&N);
    while (N--) {
        scanf("%d %d",&m,&V);
        memset(f, 0x8f, sizeof(f));
        f[0]=0;
        for (int i = 1; i <= m; i++) {
            scanf("%d %d", &c[i], &w[i]);
        }
        for (int i = 1; i <= m; i++) {
            completePack(V, i);
        }
        if (f[V] > 0) {
            printf("%d\n",f[V]);
        }else{
            printf("NO\n");
        }
    }
    //fclose(stdin);
    return 0;
}

 

posted @ 2018-04-13 10:30  zhaoGavin  阅读(98)  评论(0编辑  收藏  举报