[英]Using custom comparator for priority_queue increases time C++
For this question on leetcode, using dijkstra
algo :对于 leetcode 上的这个问题,使用
dijkstra
算法:
You are a hiker preparing for an upcoming hike.
你是一名徒步旅行者,为即将到来的徒步旅行做准备。 You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col).
给定高度,一个大小为行 x 列的二维数组,其中 heights[row][col] 表示单元格的高度(row, col)。 You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (ie, 0-indexed).
您位于左上角的单元格 (0, 0),您希望移动到右下角的单元格 (rows-1, columns-1)(即 0 索引)。 You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.
您可以向上、向下、向左或向右移动,并且您希望找到一条需要最少努力的路线。
A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.
路线的努力是路线的两个连续像元之间高度的最大绝对差。
Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
返回从左上角单元格移动到右下角单元格所需的最小努力。
I am getting TLE
time limit exceeded if I use priority_queue
with struct
.如果我将
priority_queue
与struct
一起使用,则会超出TLE
时间限制。 Can anyone help me figure out the reason?谁能帮我找出原因?
TLE code : TLE 代码:
struct heapNode
{
int w;
int i;
int j;
heapNode(int n1, int n2, int n3) : w(n1), i(n2), j(n3)
{
}
};
struct Comp {
bool operator()(heapNode const& p1, heapNode const& p2)
{
// return "true" if "p1" is ordered
// before "p2", for example:
return p1.w < p2.w;
}
};
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
int r = heights.size(), c = heights[0].size();
int dirs[5] = {-1, 0, 1, 0, -1};
priority_queue<heapNode,vector<heapNode>,Comp> minheap;
vector<vector<int>> effort(r,vector<int>(c,INT_MAX));
// effort[0][0] = 0;
minheap.push(heapNode(0,0,0));
while(!minheap.empty())
{
auto top = minheap.top(); minheap.pop();
int w = top.w;
int i = top.i;
int j = top.j;
if (w >= effort[i][j]) continue;
effort[i][j] = w;
for(int k = 0;k<4;k++)
{
int x = i + dirs[k], y = j + dirs[k + 1];
if(x>=0 && y>=0 && x<r && y<c)
{
int t = max(w, abs(heights[i][j] - heights[x][y]));
minheap.push(heapNode(t,x,y));
}
}
}
return effort[r-1][c-1];
}
};
Working Code :工作代码:
using pii = pair<int,int>;
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
int r = heights.size(), c = heights[0].size();
int dirs[5] = {-1, 0, 1, 0, -1};
priority_queue<pii,vector<pii>,greater<pii>> minheap;
vector<vector<int>> effort(r,vector<int>(c,INT_MAX));
effort[0][0] = 0;
minheap.push({effort[0][0],0});
while(!minheap.empty())
{
auto top = minheap.top(); minheap.pop();
int w = top.first;
int i = top.second / 100, j = top.second % 100;
for(int k = 0;k<4;k++)
{
int x = i + dirs[k], y = j + dirs[k + 1];
if(x>=0 && y>=0 && x<r && y<c)
{
if(effort[x][y] > max(w, abs(heights[i][j] - heights[x][y])))
{
effort[x][y] = max(w, abs(heights[i][j] - heights[x][y]));
minheap.push({effort[x][y],x*100+y});
}
}
}
}
return effort[r-1][c-1];
}
};
Your question does not describe how "effort" is to be calculated.您的问题没有描述如何计算“努力”。
Assuming that effort to travel from adjacent cells a,b is calculated as假设从相邻单元 a,b 移动的努力计算为
sqrt( 1 + ( height(a) - (height(b) )^2 )
An application to solve this problem is described here https://github.com/JamesBremner/PathFinder2/wiki/Hills此处描述了解决此问题的应用程序https://github.com/JamesBremner/PathFinder2/wiki/Hills
There are two steps.有两个步骤。
Convert the orthogonal grid to a graph with costed links将正交网格转换为具有成本链接的图
Run Dijsktra to find the cheapest route from source to destination运行 Dijsktra 以查找从源到目的地的最便宜的路线
The github repo linked to above has c++ code that can solve this very quickly ( one hundred thousand cells in less than a second. )上面链接的 github 存储库有 C++ 代码,可以非常快速地解决这个问题(不到一秒钟就可以解决十万个单元。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.