Fibonacci Knapsack
生活随笔
收集整理的這篇文章主要介紹了
Fibonacci Knapsack
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?http://acm.uestc.edu.cn/problem.php?pid=1489&cid=164
其實(shí)就是用搜索做0/1背包
不要被Fibonacci 唬住了,沒什么用。,。。。。。這個(gè)比較坑爹
剪枝在代碼中說明了
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | ? #include "StdAfx.h" #include <iostream> #include <stdio.h> #include <memory.h> #include <string> #include <queue> #include <math.h> #include<cstdlib> #include<algorithm> using namespace std; #define LL long long LL n,w,maxx; LL f[60],ww[60]; LL m[51][51]; struct node {LL c,w; }; node a[60]; bool cmp(node a,node b) {if (a.w!=b.w) return a.w>b.w;else return a.c>b.c; }void find(int i,LL w,LL c,int flag) { if (c>maxx) maxx=c;if (i>=n) return ;//后面全選都沒用if (f[n-1]-f[i]+a[i].c+c<=maxx) return;//后面全選都不會(huì)超重if (f[n-1]-f[i]+a[i].c+c>=maxx && ww[n-1]-ww[i]+a[i].w<=w){maxx=f[n-1]-f[i]+a[i].c+c;return ;}if (flag==0 && a[i].w==a[i-1].w) //上一個(gè)和a[i]等重而且價(jià)值更高的都沒選,肯定不會(huì)選a[i]{//不選find(i+1,w,c,0);return ;}else{//不選find(i+1,w,c,0);//選if (w<a[i].w) return ; //超重了find(i+1,w-a[i].w,c+a[i].c,1); }}int main() {int t;cin>>t;for (int tt=0;tt<t;tt++){cin>>n>>w;memset(a,0,sizeof(a));memset(m,0,sizeof(m));memset(f,0,sizeof(f));memset(ww,0,sizeof(ww));for (int i=0;i<n;i++){cin>>a[i].w>>a[i].c;}maxx=0;sort(a,a+n,cmp);f[0]=a[0].c;ww[0]=a[0].w;for (int i=1;i<n;i++){f[i]=f[i-1]+a[i].c;ww[i]=ww[i-1]+a[i].w;}find(0,w,0,1);cout<<"Case "<<tt+1<<": "<<maxx<<endl;}return 0; } |
轉(zhuǎn)載于:https://blog.51cto.com/darkscope/989019
總結(jié)
以上是生活随笔為你收集整理的Fibonacci Knapsack的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微博html5版是电脑还是手机,搜狐微博
- 下一篇: foursquare nyc数据集_炫酷