【发布时间】:2015-03-09 10:42:11
【问题描述】:
我正在尝试对图形边缘进行排序,但无法这样做。 下面给出的是我实现相同的优先级队列。
class CompareDistance
{
public:
bool operator()(pair<int,int> n1,pair<int,int> n2)
{
if(g[n1.first][n2.second] < g[n2.first][n2.second])
return true;
else
return false;
}
};
int g[3][3] = {{4,1,3},{3,3,3},{3,3,3}};//graph edges
int main()
{
priority_queue<pair<int,int>,vector<pair<int,int> >,CompareDistance> pq;
for(int i = 0 ; i < 3 ; i++)
for(int j = 0 ; j < 3 ; j++)
pq.push(pair<int,int>(i,j));
cout<<"\t"<<g[pq.top().first][pq.top().second];//does not give the correct result
pq.pop();
getch();
}
【问题讨论】:
-
请说明你的目标是什么,并提出一个问题...
-
为了更好地理解,我对其进行了编辑,我希望优先级队列在图中包含按距离排序的节点值对-@WojciechFrohmberg
-
@HARJATINSINGH 您是否有充分的理由尝试将它们全部放入 PQ 中,而不是简单地制作一个向量并对其进行排序?
标签: c++ graph stl priority-queue