POJ 1276 (multiple backpack + closest)

Source: Internet
Author: User

Http://www.cnblogs.com/rainydays/archive/2013/03/08/2950258.html

Http://www.cnblogs.com/ziyi--caolu/p/3216827.html

The first type:

adjacency + Statistic Quantity

#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<vector>#include<iterator>#include<Set>#include<map>#include<sstream>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#definePB Push_back#defineDebug printf ("!\n")#defineMAXN 1010#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Long#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definePqueue priority_queue#defineINF 0x3f3f3f3fintcash,n,sum;intc[ the],w[ the],used[100005];BOOLf[100005];voidmubag () {inti,j; Mem (F,false); f[0] =true; //Debug;     for(i=0; i<n;i++) {mem (used,0);  for(j = w[i];j<=cash;j++)        {            if(F[j-w[i] &&!f[j] && used[j-w[i]]<C[i]) {F[j]=true; USED[J]= used[j-w[i]]+1; //PF ("f%d cnt%d\n", J,used[j]);            }        }    }}intMain () {inti,j;  while(~SF ("%d%d",&cash,&N)) { for(i=0; i<n;i++) {SF ("%d%d",&c[i],&W[i]);        } mubag ();  for(i=cash;i>=0; i--)        {            if(F[i]) {PF ("%d\n", i);  Break; }        }    }    return 0;}

The second type:

Binary split +01 backpack

Value = Weight

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;intdp[110000],t[30000],s[ -][2];intMain () {intCash,n;  while(SCANF ("%d%d", &cash,&n) >0)    {         intCnt=0; //memset (t,0,sizeof (t));          for(intI=1; i<=n;i++) {scanf ("%d%d", &s[i][0],&s[i][1]); intk=1; if(s[i][0]==0|| s[i][1]==0)                 Continue;  while(s[i][0]-k>0) {t[cnt++]=k*s[i][1]; s[i][0]-=K; K*=2; } t[cnt++]=s[i][0]*s[i][1]; } memset (DP,0,sizeof(DP));  for(intI=0; i<cnt;i++)         {                  for(intj=cash;j>=t[i];j--)                 if(dp[j]<dp[j-t[i]]+T[i]) dp[j]=dp[j-t[i]]+T[i]; } printf ("%d\n", Dp[cash]); }    return 0;}

The third type:

Full Backpack + Statistics quantity

Value = Weight

Basically the same as the first, the speed is the same, a little clearer

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;intdp[110000],num[100100],s[ -][2];intMain () {intCash,n;  while(SCANF ("%d%d", &cash,&n) >0)    {         intCnt=0;  for(intI=1; i<=n;i++) {scanf ("%d%d", &s[i][0],&s[i][1]); } memset (DP,0,sizeof(DP));  for(intI=1; i<=n;i++) {memset (num,0,sizeof(num));  for(intj=s[i][1];j<=cash;j++)                 if(dp[j]<dp[j-s[i][1]]+s[i][1]&&num[j-s[i][1]]<s[i][0]) {Dp[j]=dp[j-s[i][1]]+s[i][1]; NUM[J]=num[j-s[i][1]]+1; }} printf ("%d\n", Dp[cash]); }    return 0;}

POJ 1276 (multiple backpack + closest)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.