首页 > 代码库 > XDOJ_1036_01背包

XDOJ_1036_01背包

http://acm.xidian.edu.cn/problem.php?id=1036

 

先求总sum,对sum/2进行背包算法即可。

 

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int n,w[205],dp[200005];int main(){    int T;    scanf("%d",&T);    while(T--)    {        memset(dp,0,sizeof(dp));        int sum = 0;        scanf("%d",&n);        for(int i = 1;i <= n;i++)        {            scanf("%d",&w[i]);            sum += w[i];        }        int endd = sum/2;        for(int i = 1;i <= n;i++)        {            for(int j = endd;j >= w[i];j--)            {                dp[j] = max(dp[j],dp[j-w[i]]+w[i]);            }        }        printf("%d\n",sum-dp[endd]*2);    }    return 0;}

 

XDOJ_1036_01背包