如何在C++中正确使用优先级队列
How to properly use priority queues in C++
对于数据结构类中的赋值,我假设为所谓的背包问题实现分支和绑定方法。问题本身不是问题,但如果您不知道这是问题,则问题涉及查找包含最大利润的项目集,因为项目的总重量不超过变量 W。代码中注释中有关该问题的更多信息。我的问题是,当尝试使用推送功能将节点插入优先级队列时,它会给我一些错误消息,如下所示
no matching function for call to ‘std::priority_queue<int>::push(node&)’
我过去使用过队列,但不是优先级队列,我已经对如何在C++中使用它们进行了大量研究。但是,似乎没有什么对我有用。如果有人能指出我在使用优先级队列方面做错了什么,我将不胜感激。这是我的代码,请注意,所有函数都在一个名为 knapsack3.cpp 的文件中:
主要功能
/*Let n items be given, where each item has a weight and a profit. The weights
and profits as positive integers. Furthermore, let a positive interger W be given.
Determine a set of items with the maximum total profit, under the condition that
the sum of their weights cannot exceed W. */
//Preprocessor Directives
#include <stdio.h>
#include <queue>
#include <iostream>
//node data structure
struct node { int level; int profit; int weight; float bound; };
using namespace std;
//function protocol
void knapsack3(int n, int p[], int w[], int W, int* maxprofit);
float bound(int n, int p[], int w[], int W, node u);
//declare arrays
int p[5]; //holds profit values for 5 items
int w[5]; //hold weight values for 5 items
int main() {
//declare variables
int n = 5; int i; int W = 13; int maxprofit = 0;
//enter values for profit and weight in a way that each of which is sorted in
//decreasing order according to the values of p[i]/w[i]
printf("Enter Profitsn");
for(i=0; i<n; i++)
scanf("%d", &p[i]);
printf("Enter Weightsn");
for(i=0; i<n; i++)
scanf("%d", &w[i]);
std::queue<int> PQ;
knapsack3(n, p, w, W, &maxprofit);
//print value for maxprofit
printf("%dn", maxprofit); }
背包功能
//function that finds the maxprofit that is the sum of the profits of an optimal set.
void knapsack3(int n, int p[], int w[], int W, int* maxprofit)
{
//initialize priority queue PQ
priority_queue<int>PQ;
//initialize nodes u and v
node u, v;
//initialize PQ to be empty
v.level = 0; v.profit = 0; v.weight = 0;
maxprofit = 0;
//initialize v to be the root
v.bound = bound(n, p, w, W, v);
PQ.push(v);
//remove node with best bound
while(!empty(PQ)){
PQ.pop();
//check if node is still promising
if(v.bound > maxprofit){
u.level = v.level + 1;
//set node u to the child node that includes the next item
u.weight = v.weight + w[u.level];
u.profit = v.profit + p[u.level];
if(u.weight <= W && u.profit > maxprofit)
maxprofit = u.profit;
u.bound = bound(n, p, w, W, u);
if(u.bound > maxprofit)
PQ.push(u);
//set node u to the child node that does not include the next item
u.weight = v.weight;
u.profit = v.profit;
u.bound = bound(n, p, w, W, u);
if(u.bound > maxprofit)
PQ.push(u);
}
}
}
绑定函数
//function that returns the bound of a node
float bound(int n, int p[], int w[], int W, node u)
{
int j, k;
int totalweight;
float result;
if(u.weight >=W)
return 0;
else{
result = u.profit;
j = u.level + 1;
totalweight + u.weight;
while(j<=n && totalweight + w[j] <= W){
totalweight = totalweight + w[j];
result = result p[j];
j++;
}
k=j;
if(k<=n)
result = result + (W-totalweight) * p[k]/w[k];
return result;
}
}
您正在尝试将struct node
推送到键入int
的队列中。尝试将 PQ 类型更改为priority_queue<node>
相关文章:
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 打印优先级队列
- 带自定义比较器的最小优先级队列
- 优先级队列自定义比较器
- 排序数组优先级队列
- 更改运行时优先级队列的排序功能
- 优先级队列构造函数的工作
- 实现优先级队列
- 优先级队列功能比较
- 在C++中打印对的优先级队列的所有值时出现问题
- 使用堆的优先级队列,具有相同键的值不遵循 FIFO(先进先出)
- 为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?
- 是否可以使用简单队列创建优先级队列
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- Cython中带有自定义比较器的优先级队列
- 优先级队列比较器[C++].
- asio::io_service 具有多个线程的优先级队列处理