日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Fibonacci Knapsack

發(fā)布時(shí)間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fibonacci Knapsack 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?http://acm.uestc.edu.cn/problem.php?pid=1489&cid=164

其實(shí)就是用搜索做0/1背包
不要被Fibonacci 唬住了,沒什么用。,。。。。。這個(gè)比較坑爹
剪枝在代碼中說明了

?View Code?C
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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。