【背包总结】【输出方案总数】

来源:互联网 发布:mac上面怎么装office 编辑:程序博客网 时间:2024/05/21 19:33
#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;    int n,tot;const int N = 10;const int M = 1000;int dp[M + 1];int c[M + 1];int v[N];int w[N];int main(){    while(scanf("%d%d",&n,&tot) != EOF){memset(dp,0,sizeof(dp));memset(c,0,sizeof(c));c[0] = 1;for(int i=0;i<n;i++){   scanf("%d%d",&w[i],&v[i]);}for(int i=0;i<n;i++){for(int j=tot;j>=w[i];j--){if(dp[j] == dp[j-w[i]] + v[i]){c[j] += c[j-w[i]];}else if(dp[j] < dp[j-w[i]] + v[i]){dp[j] = dp[j-w[i]] + v[i];c[j] = c[j-w[i]];}else{//c[j] = c[j];}}}cout << c[tot] << endl;}    return 0;}

0 0
原创粉丝点击