POJ 2184 Cow Exhibition(01背包变形 or dfs+剪枝)

来源:互联网 发布:微信全网发布封域名 编辑:程序博客网 时间:2024/06/10 23:01
Cow Exhibition

Time Limit: 1000MS


Memory Limit: 65536K

Total Submissions: 10929


Accepted: 4338

Description

"Fat and docile, big and dumb, they look so stupid, they aren't much 
fun..." 
- Cows with Guns by Dana Lyons 

The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow. 

Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative. 

Input

* Line 1: A single integer N, the number of cows 

* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow. 

Output

* Line 1: One integer: the optimal sum of TS and TF such that both TS and TF are non-negative. If no subset of the cows has non-negative TS and non- negative TF, print 0. 

Sample Input

5-5 78 -66 -32 1-8 -5

Sample Output

8

Hint

OUTPUT DETAILS: 

Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF 
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value 
of TS+TF to 10, but the new value of TF would be negative, so it is not 
allowed. 

题意:有n头奶牛,每个奶牛有智慧si和幽默fi这两个属性,现在需要在这群奶牛里面挑选奶牛参加展览,要求在满足sum_s和sum_f都大于等于0的情况下,使得sum_s+sum_f的值最大,最大值是多少?

题解: 对于每种奶牛都是取与不取的情况,对于这种问题与背包的模型很接近,这一题就是一个01背包变形。题中的参数值存在负数,需要把这个负数情况也计入dp数组下标,就要将所有值加上负数最大值。 用dp[i]存放每个s[i]能得到的最佳F,那么我们就可以根据s[i]的取值采取两种不同的01背包取法,在取完之后,然后再根据背包的有无再去求得最佳答案即可

代码如下:

//dp解法:01背包变形 #include<cstdio>#include<cstring>#include<algorithm>#define INF 0x3f3f3fusing namespace std;int dp[200050];int s[110],f[110];int main(){int n,i,j;while(scanf("%d",&n)!=EOF){for(i=0;i<n;++i)scanf("%d%d",&s[i],&f[i]);for(i=0;i<=200000;++i)dp[i]=-INF;dp[100000]=0;for(i=0;i<n;++i){if(s[i]<0&&f[i]<0)continue;if(s[i]>0){for(j=200000;j>=s[i];--j)dp[j]=max(dp[j],dp[j-s[i]]+f[i]);}else//负数需要反过来 {for(j=s[i];j<=200000+s[i];j++)dp[j]=max(dp[j],dp[j-s[i]]+f[i]);}}int ans=-INF;for(i=100000;i<=200000;++i)//因为区间100000~200000才是表示的整数,那么此时的i就是之前背包中的s[i],如果此时dp[i]也就是f[i]大于等于0的话,我们再加上s[i](此时为i),然后减去作为界限的100000,就可以得到答案  if(dp[i]>=0)ans=max(ans,dp[i]+i-100000);printf("%d\n",ans);}return 0;}


这一题除了dp解法,还可以dfs深搜,每头奶牛都只有取与不取的情况,所以最多情况共有2^100种,显然这样直接搜会超时的,但这一题有很多可以剪枝的地方,具体看代码吧。

如下:

//dfs+剪枝解法 #include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{int s,f;}cow[110];int n,sum_s,sum_f,ans;int sum[110];int cmp(node a,node b){return a.s+a.f>b.s+b.f;}void dfs(int x){if(x==n){if(sum_s>=0&&sum_f>=0)ans=max(sum_s+sum_f,ans);//更新最大值 return ;}if(cow[x].s+cow[x].f<0&&sum_s+sum_f<ans)return ;if(sum_s+sum_f+sum[x]<=ans)return ;sum_s+=cow[x].s;sum_f+=cow[x].f;dfs(x+1);sum_s-=cow[x].s;sum_f-=cow[x].f;dfs(x+1);}int main(){int i;while(scanf("%d",&n)!=EOF){ sum_s=0; sum_f=0;ans=0;for(i=0;i<n;++i){scanf("%d%d",&cow[i].s,&cow[i].f);if(cow[i].s>=0&&cow[i].f>=0)//都是正数,直接取用 {sum_s+=cow[i].s;sum_f+=cow[i].f;i--;n--;}else if(cow[i].s<0&&cow[i].f<0)//两个性质都是负数,直接不计入 {i--;n--;continue;}}ans=sum_s+sum_f;sort(cow,cow+n,cmp);memset(sum,0,sizeof(sum));for(i=n-1;i>=0;--i){if(cow[i].s+cow[i].f<=0)sum[i]=0;elsesum[i]=sum[i+1]+cow[i].s+cow[i].f;//记录每个牛前面牛的总值 }dfs(0);printf("%d\n",ans); }return 0;}






1 0