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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

背包问题 小灰_小背包问题

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

背包問(wèn)題 小灰

Prerequisites: Algorithm for fractional knapsack problem

先決條件: 分?jǐn)?shù)背包問(wèn)題算法

Here, we are discussing the practical implementation of the fractional knapsack problem. It can be solved using the greedy approach and in fractional knapsack problem, we can break items i.e we can take a fraction of an item. For examples, you can read this article first.

在這里,我們正在討論小背包問(wèn)題的實(shí)際實(shí)現(xiàn) 。 可以使用貪婪方法解決該問(wèn)題,在小背包問(wèn)題中,我們可以破壞物品,即可以取物品的一小部分。 例如,您可以先閱讀本文 。

Problem statement: We have given items i1, i2, ..., in (item we want to put in our bag) with associated weights w1, w2, ..., wn and profit values P1 , P2 ,..., Pn. Now problem is how we can maximize the total benefit given capacity of bag is C?

問(wèn)題陳述:我們給了項(xiàng)目i 1 , i 2 ,..., i n (我們要放入袋中的項(xiàng)目),其權(quán)重為w 1 , w 2 ,..., w n和利潤(rùn)值P 1 , P 2 ,..., P n 。 現(xiàn)在的問(wèn)題是,在袋子容量為C的情況下, 如何才能使總收益最大化 ?

Algorithm:

算法:

  • Compute the profit per weight density for each item using the formula di = Pi / wi.

    使用公式d i = P i / w i計(jì)算每個(gè)項(xiàng)目的每重量密度利潤(rùn)。

  • Sort each item by its profit per weight density.

    按每重量密度的利潤(rùn)對(duì)每個(gè)項(xiàng)目進(jìn)行排序。

  • Maximize the profit i.e Take as much as possible of the profit per weight density item not already in the bag.

    最大化利潤(rùn),即盡可能多地利用袋中尚未存在的每重量密度物品的利潤(rùn)。

  • 分?jǐn)?shù)背負(fù)問(wèn)題的C ++實(shí)現(xiàn) (C++ implementation of fractional knapsack problem)

    #include <bits/stdc++.h> using namespace std; // Structure for an item struct myItem {int itemNo; int profit;int weight;float ppw; // profit per weight }; // Comparison function to sort Item according to profit per weight ratio bool cmp(struct myItem a, struct myItem b) { return a.ppw > b.ppw; } float fractionalKnapsack(int Capacity, struct myItem arr[], int n) { //calculating profit per weight ratiofor(int i=0;i<n;i++){arr[i].ppw = ((float)arr[i].profit / arr[i].weight);}// sorting Item on basis of profit per weight ratio sort(arr, arr + n, cmp); cout<<"details of all items : \n";cout<<"itemNo\t"<<"Profit\t"<<"Weight\t"<<"PPW\t"<<endl;for (int i = 0; i < n; i++) { cout <<arr[i].itemNo<<"\t"<<arr[i].profit<<"\t"<<arr[i].weight<<"\t"<<((float)arr[i].ppw)<<endl; }cout<<endl;float Max = 0.0; // Maximum profitint i=0; //take items until capacity becomes zerowhile(Capacity > 0 && i<=n-1){// if we can take all weights of itemif(Capacity >= arr[i].weight){Max = Max + (float)arr[i].profit;Capacity = Capacity - arr[i].weight;}// we can take only fraction of itemelse{Max += (Capacity * arr[i].ppw);Capacity = 0;}i++;} return Max; } // driver function int main() { int C = 25; // Capacity of knapsack myItem arr[] = { {1, 30, 10, 0}, {2, 20, 5, 0} , {3, 40, 15, 0}, {4, 36, 8, 0}}; int n = sizeof(arr) / sizeof(arr[0]);cout<<"details of all items before sorting and without calculating PPW: \n";cout<<"itemNo\t"<<"Profit\t"<<"Weight\t"<<"PPW\t"<<endl;for (int i = 0; i < n; i++) { cout <<arr[i].itemNo<<"\t"<<arr[i].profit<<"\t"<<arr[i].weight<<"\t"<<((float)arr[i].ppw)<<endl; } cout<<endl;cout << "Maximum profit we can obtain = "<< fractionalKnapsack(C, arr, n);return 0; }

    Output

    輸出量

    details of all items before sorting and without calculating PPW: itemNo Profit Weight PPW 1 30 10 0 2 20 5 0 3 40 15 0 4 36 8 0details of all items : itemNo Profit Weight PPW 4 36 8 4.5 2 20 5 4 1 30 10 3 3 40 15 2.66667Maximum profit we can obtain = 91.3333

    翻譯自: https://www.includehelp.com/icp/fractional-knapsack-problem.aspx

    背包問(wèn)題 小灰

    總結(jié)

    以上是生活随笔為你收集整理的背包问题 小灰_小背包问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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