P1504 building blocks castle
answer
Type reach 01 backpack
For each set of the castle, which can reach some height
But we are asking for is the maximum height of all public backpack can be reached
f [i] represents a set for the castle, can reach the height j, then we ran n times
g [i] represents all the castle, you can reach the height of the j
Code
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<string> #include<cstring> #include<queue> using namespace std; typedef long long ll; inline int read() { int ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } bool f[10005],g[10005]; int n,a[10005],tot=0,sum=0,ans=0; int main() { n=read(); for(int i=1;i<=10005;i++) g[i]=1; for(int t=1;t<=n;t++){ tot=0;sum=0; memset(a,0,sizeof(a)); memset(f,0,sizeof(f)); while(a[++tot]=read()){ if(a[tot]==-1) { tot--; break; } sum+=a[tot]; } f[0]=1; for(int i=1;i<=tot;i++) for(int j=sum;j>=a[i];j--) f[j]|=f[j-a[i]]; for(int i=1;i<=10005;i++) g[i]=g[i]&f[i]; } for(int i=0;i<=10005;i++) if(g[i]) ans=i; printf("%d\n",ans); return 0; }