ZJU 3357 Present for MM 【活用背包算法】

  

浙大月赛题目

Present for MM


Time Limit: 10 Seconds      Memory Limit: 32768 KB

 


 

http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=3557

One day, DD finds a strange hole with many treasures in it. He realizes that all the treasures are in pairs. Through deeply investigation, DD finds that there is some relation between each pair of treasures. One relation is just like DD and his girlfriend MM, if they are separated, both of them will lose values. The second relation is like DD and GG (a boy who loves MM), if they get together, a terrible thing will hapen, both treasures will lose values. The third relation is like DD and his shadow, it means that DD can't only choose shadow without himself, but DD can just choose himself without his shadow.

Every treasure has its own weight (Wi) and value (Vi), DD just has a broken package, it only allows W (or less) weight. And each treasures is unique, it means that DD can't choose a treasure twice. Another strange thing is all the weight of treasures are multiples of 100 (No one knows the reason).

Your task is to help DD to choose the treasures, DD wants the most values of treasure, so he can buy more present for MM.

Input

 

 

The input file will contain multiple test cases. The first line contains 2 integers W (0 <= W <= 5000000), N (0 <= N <= 10000). Following is N lines, each line contains 5 integers about two treasures' information and their relation, for example the ith line contains W2i-1, V2i-1, W2i, V2i, Ri (Ri=1 means the relation is like DD and MM, Ri=2 means the relation is like DD and GG, Ri=3 means the relation is like DD and his shadowm, the (2i-1)-th is like DD, and the (2i)-th is like his shadow). (We have 0 <= Vi <= 1000)

Output

 

 

For each case, you should print a single line with a single integer, the max value DD can have.

Sample Input

 

 

200 1
200 1 100 5 3
1000 3
200 2 300 3 1
300 5 300 6 2
600 1 200 5 3

Sample Output

 

 

1
11
 

 

我的未AC的代码:主要一个问题就是放进去拿出来不知道  隐藏的吧 怎么解决啊?????晕啊 ~~~·

 #include #include #include #define max(x,y) x>y?x:y typedef struct { int a,c1,b,c2; int key; }node; node pack[10010]; int c,f[50005]; int cmp(void const *a,void const *b) { return (*(node*)a).key>(*(node*)b).key?1:-1; } void zeroonepack(int cost,int weight) { int i; for(i=c;i>=weight;i--) { f[i]=max(f[i-weight]+cost,f[i]); } } int main() { int n,i,top; while(scanf("%d%d",&c,&n)!=EOF) { top=0; c=c/100; memset(f,0,sizeof(f[0])*(c+1)); for(i=0;i

后来请教了下topsky  解决了~~感谢他的指点 我把算法用死 了  要活用~~  非常感谢他

好了 看看 我后来的代码吧

 

 #include #include int f[50005]; int main() { int c,n,j,max,i; int c1,c2,v1,v2,x,tem,tem2; while(scanf("%d%d",&c,&n)!=EOF) { c/=100; max=0; memset(f,0,sizeof(f[0])*(c+1)); while(n--) { scanf("%d%d%d%d%d",&c1,&v1,&c2,&v2,&x); c1/=100; c2/=100; if(x==3)//自己与自己+影子 { c2+=c1; v2+=v1; } if(x==1)//两个同时 那么合并成一个即可 { c1+=c2; v1+=v2; c2=0; v2=0; } if( c1 > c2 )//保证在某状态只放一个进去 并且大的那个 { tem = c1; c1 = c2; c2 = tem; tem = v1; v1 = v2; v2 = tem; } for( j=c; j>=c2; --j ) { tem = f[j-c1] + v1; tem2 = f[j-c2] + v2; if( tem < tem2 ) { tem = tem2; } if( tem > f[j] ) { f[j] = tem; } } for( ; j>=c1; --j ) { if( ( tem = f[j-c1] + v1 ) > f[j] ) { f[j] = tem; } } } for(i=1;i<=c;i++) if(max

你可能感兴趣的:(judge,online)