首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在C++中正确使用优先级队列

如何在C++中正确使用优先级队列
EN

Stack Overflow用户
提问于 2014-11-15 04:16:35
回答 1查看 908关注 0票数 1

对于我的数据结构类中的赋值,我应该为所谓的背包问题实现分支和绑定方法。问题本身并不是问题所在,但如果您不知道问题所在,则需要查找包含最大利润的项目集,因为项目的总权重不超过变量W。代码中注释的有关问题的更多信息。我的问题是,当尝试使用push函数将一个节点插入优先级队列时,它会给出一些类似于下面的错误消息

no matching function for call to ‘std::priority_queue<int>::push(node&)’

我过去处理过队列,但没有使用优先级队列,我已经对如何在C++中使用队列进行了大量的研究。然而,似乎没有什么对我有用。如果有人能指出我在使用优先级队列方面做错了什么,我将非常感激。下面是我的代码,请注意,所有函数都位于一个名为knapsack3.cpp的文件上:

主要功能

代码语言:javascript
复制
 /*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 Profits\n"); 
for(i=0; i<n; i++)
    scanf("%d", &p[i]);
printf("Enter Weights\n");
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("%d\n", maxprofit); } 

背包函数

代码语言:javascript
复制
//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);
        }
    }
}

定界函数

代码语言:javascript
复制
//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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-15 05:29:26

您正在尝试将struct node推入为int键入的队列中。尝试将PQ类型更改为priority_queue<node>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26942475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档