背包問題 小灰_小背包問題

背包問題 小灰

Prerequisites: Algorithm for fractional knapsack problem

先決條件: 分數背包問題算法

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.

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

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?

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

Algorithm:

算法:

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

    使用公式d i = P i / w i計算每個項目的每重量密度利潤。

  2. Sort each item by its profit per weight density.

    按每重量密度的利潤對每個項目進行排序。

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

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

分數背負問題的C ++實現 (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 ratio
for(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 profit
int i=0; 
//take items until capacity becomes zero
while(Capacity > 0 && i<=n-1)
{
// if we can take all weights of item
if(Capacity >= arr[i].weight)
{
Max = Max + (float)arr[i].profit;
Capacity = Capacity - arr[i].weight;
}
// we can take only fraction of item
else
{
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       0
details of all items :
itemNo  Profit  Weight  PPW
4       36      8       4.5
2       20      5       4
1       30      10      3
3       40      15      2.66667
Maximum profit we can obtain = 91.3333

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

背包問題 小灰

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377587.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377587.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377587.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

360瀏覽器兼容問題

360瀏覽器兼容問題 360瀏覽器又是一大奇葩&#xff0c;市場份額大&#xff0c;讓我們不得不也對他做些兼容性處理。 360瀏覽器提供了兩種瀏覽模式&#xff0c;極速模式和兼容模式&#xff0c;極速模式下是webkit內核的處理模式&#xff0c;兼容模式下是與IE內核相同的處理模式。…

轉 設計師也需要了解的一些前端知識

一、常見視覺效果是如何實現的 一些事 關于文字效果 互聯網的一些事 文字自身屬性相關的效果css中都是有相對應的樣式的&#xff0c;如字號、行高、加粗、傾斜、下劃線等&#xff0c;但是一些特殊的效果&#xff0c;主要表現為ps中圖層樣式中的效果&#xff0c;css是無能為力的…

六、DataLoader

一、DataLoader參數解析 DataLoader官網使用手冊 參數描述dataset說明數據集所在的位置、數據總數等batch_size每次取多少張圖片shuffleTrue亂序、False順序(默認)samplerbatch_samplernum_workers多進程&#xff0c;默認為0采用主進程加載數據collate_fnpin_memorydrop_las…

單調棧 leetcode整理(一)

目錄單調棧知識402. 移掉K位數字1673. 找出最具競爭力的子序列316. 去除重復字母&#xff08;1081. 不同字符的最小子序列&#xff09;321. 拼接最大數單調棧知識 單調棧就是一個內部元素有序的棧&#xff08;大->小 or 小->大&#xff09;&#xff0c;但是只用到它的一…

數字簽名 那些密碼技術_密碼學中的數字簽名

數字簽名 那些密碼技術A signature is usually used to bind signatory to the message. The digital signature is thus a technique that binds a person or the entity to the digital data. This binding ensures that the person sending the data is solely responsible …

七、torch.nn

一、神經網絡模塊 進入到PyTorch的torch.nnAPI學習頁面 PyTorch提供了很多的神經網絡方面的模塊&#xff0c;NN就是Neural Networks的簡稱 二、Containers torch.nn下的Containers 一共有六個模塊&#xff0c;最常用的就是Module模塊&#xff0c;看解釋可以知道&#xff0c…

Java多線程初學者指南(8):從線程返回數據的兩種方法

本文介紹學習Java多線程中需要學習的從線程返回數據的兩種方法。從線程中返回數據和向線程傳遞數據類似。也可以通過類成員以及回調函數來返回數據。原文鏈接 從線程中返回數據和向線程傳遞數據類似。也可以通過類成員以及回調函數來返回數據。但類成員在返回數據和傳遞數據時有…

【C++進階】 遵循TDD原則,實現平面向量類(Vec2D)

目錄1、明確要實現的類的方法以及成員函數2、假設已經編寫Vec2D&#xff0c;根據要求&#xff0c;寫出測試代碼3、編寫平面向量類Vec2D,并進行測試4、完整代碼5、最終結果1、明確要實現的類的方法以及成員函數 考慮到效率問題&#xff0c;我們一般將函數的參數設置為引用類型。…

Keilc的中斷號計算方法

中斷號碼 (中斷向量-3)/8轉載于:https://www.cnblogs.com/yuqilihualuo/p/3423634.html

md5模式 簽名_MD的完整形式是什么?

md5模式 簽名醫師&#xff1a;醫學博士/常務董事 (MD: Doctor of Medicine / Managing Director) 1)醫學博士&#xff1a;醫學博士 (1) MD: Doctor of Medicine) MD is an abbreviation of a Doctor of Medicine degree. In the field of Medicine, it is the main academic de…

八、卷積層

一、Conv2d torch.nn.Conv2d官網文檔 torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride1, padding0, dilation1, groups1, biasTrue, padding_modezeros, deviceNone, dtypeNone) 參數解釋官網詳情說明in_channels輸入的通道數&#xff0c;如果是彩色照片通道…

HTMl5結構元素:header

頁眉header 頁眉將是頁面加載的第一個元素&#xff0c;包含了站點的標題、logo、網站導航等。<header> <div class"container_16"> <div class"logo"> <h1><a href"index.html"><strong>Real</st…

【C++grammar】左值、右值和將亡值

目錄C03的左值和右值C11的左值和右值將亡值在C03中就有相關的概念 C03的左值和右值 通俗的理解&#xff1a; (1) 能放在等號左邊的是lvalue (2) 只能放在等號右邊的是rvalue (3) lvalue可以作為rvalue使用 對于第三點可以舉個例子&#xff1a; int x ; x 6; //x是左值&#…

scala字符串的拉鏈操作_在Scala中對字符串進行操作

scala字符串的拉鏈操作Scala字符串操作 (Scala strings operation) A string is a very important datatype in Scala. This is why there are a lot of operations that can be done on the string object. Since the regular operations like addition, subtraction is not v…

九、池化層

一、Pooling layers Pooling layers官網文檔 MaxPool最大池化層下采樣 MaxUnpool最大池化層上采樣 AvgPool最大池化層平均采樣 例如&#xff1a;池化核為(3,3)&#xff0c;輸入圖像為(5,5)&#xff0c;步長為1&#xff0c;不加邊 最大池化就是選出在池化核為單位圖像中的最大…

[分享]SharePoint移動設備解決方案

老外寫的一個PPT&#xff0c;講SharePoint在移動領域的應用&#xff0c;2012年最新的&#xff0c;有iPad喲。/Files/zhaojunqi/SharePoint2010andMobileDevices.pdf 轉載于:https://www.cnblogs.com/zhaojunqi/archive/2012/04/12/2444712.html

十、非線性激活函數

一、ReLU torch.nn.ReLU(inplaceFalse)官網提供的API 其中inplace表示是否在對原始數據進行替換 由函數圖可以看出&#xff0c;負數通過ReLU之后會變成0&#xff0c;正數則不發生變化 例如&#xff1a;input -1&#xff0c;若inplace True&#xff0c;表示對原始輸入數據進…

最短公共子序列_最短公共超序列

最短公共子序列Problem statement: 問題陳述&#xff1a; Given two strings, you have to find the shortest common super sequence between them and print the length of the super sequence. 給定兩個字符串&#xff0c;您必須找到它們之間最短的公共超級序列&#xff0c…

單調棧 leetcode整理(二)

目錄為什么單調棧的時間復雜度是O(n)496. 下一個更大元素 I方法一&#xff1a;暴力方法二:單調棧哈希表739. 每日溫度單調棧模版解優化503. 下一個更大元素 II單調棧循環遍歷為什么單調棧的時間復雜度是O(n) 盡管for 循環里面還有while 循環&#xff0c;但是里面的while最多執…

Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解決辦法)

ZZ&#xff1a;http://www.blogjava.net/anchor110/articles/355699.html1、在工程下新建lib文件夾&#xff0c;將需要的第三方包拷貝進來。2、將引用的第三方包&#xff0c;添加進工作的build path。3、&#xff08;關鍵的一步&#xff09;將lib設為源文件夾。如果不設置&…