为什么 Djikstra 算法的这段代码显示使用堆的编译错误?

Why this code for Djikstra's algorithm shows a compilation error using a heap?

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <stack>
#include <queue>
#include <climits>

using namespace std;

#define ll long long 
#define pb push_back
#define mp make_pair

vector<pair<int,int> > mygraph[100001];
int dist[100001];

void shortestpath(int n, int s);
class heapnode
{
public:
    int vertex;
    int key;
};

struct comp
{
    bool operator()(const heapnode &a, const heapnode &b)
    {
        if (a.key > b.key)
            return true;
        else
            return false;
    }
};

int main (void)
{
    int t,i,j;
    for ( i = 0; i < 100001; i++ )
        dist[i] = -1;
    int a,b,c;
    int foo,n,m,s,e;
    cin>>t;
    while (t != 0)
    {
        cin>>n>>m>>s>>e;
        foo = m;
        while (foo != 0)
        {
            cin>>a>>b>>c;
            mygraph[a].pb(mp(b,c));
            mygraph[b].pb(mp(a,c));
            foo--;
        }
        shortestpath(n,s);
        if (dist[e] == -1)
            cout<<"NONE\n";
        else
            cout<<dist[e]<<"\n";
        t--;
    }
    return 0;
}

void shortestpath(int n, int s)
{
    vector<heapnode> myvec;
    myvec.resize(n);
    int i,j,val,weight;
    for ( i = 1; i <= n; i++ )
    {
        myvec[i].vertex = i;
        myvec[i].key = INT_MAX;
    }
    myvec[s].key = 0; // setting the source key to be 0 for Djikstra's
    bool visited[n+1];
    for ( i = 1; i <= n; i++)
        visited[i] = false;  
    make_heap(myvec.begin(),myvec.end(),comp());  // making a min heap
    vector<int> processedver;
    while (processedver.size() != n)
    {
        heapnode obj = myvec.front();  
        pop_heap(myvec.begin(),myvec.end()); // popping the front
        myvec.pop_back(); 
        processedver.pb(obj.vertex); // putting the popped vertex in processedver
        dist[obj.vertex] = obj.key; // setting the value of dist
        int u = obj.vertex; 
        visited[u] = true; // setting it to be true
        auto it = mygraph[u].begin(); 
        while (it != mygraph[u].end())  // updating the vertex neighbours
        {
            for (int j = 1; j <= n; j++)
            {
                if (it->first == j)
                {
                    val = j;
                    weight = it->second;
                    break;
                }       
            }
            if (visited[val] != true && myvec[val].key > (dist[u]+weight))
            {
                myvec[val].key = dist[u]+weight;
            }
            make_heap(myvec.begin(),myvec.end(),comp());
            it++;
        }
    }
}

因此,我尝试使用最小堆实现 Dijkstra 算法。我制作了一个包含顶点号和键号的堆节点。

以上显示编译错误。 http://ideone.com/5VdCLQ

我知道这是堆的问题,但我无法正确找到问题所在。

您需要为 class heapnode 定义 operatr<(),例如:

class heapnode
{
public:
    int vertex;
    int key;
    bool operator<(const heapnode& rhs) {
        return key < rhs.key;
    }
};

您的错误源于行

    pop_heap(myvec.begin(),myvec.end());

由于 heapnode 没有 operator< 函数,您要么实施它,要么通过将 comp 更改为

将其传递给 pop_heap
    pop_heap(myvec.begin(),myvec.end(), comp());