void sol2(int k){
memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); queue<pair<int, int>> que;
sort(vec[k-1].begin(), vec[k-1].end(), [](pair<int, int> a, pair<int, int> b){return f[a.first][a.second] < f[b.first][b.second];});
for (auto i: vec[k-1]) dis[i.first][i.second] = f[i.first][i.second], que.push(i);
while (que.size()){
int x(que.front().first), y(que.front().second); que.pop(); if (vis[x][y]) continue; vis[x][y] = 1;
for (int i(0), nx, ny; i<4; ++i){
nx = x+movex[i]; ny = y+movey[i];
if (nx && ny && nx <= n && ny <= m && dis[nx][ny] > dis[x][y]+1) dis[nx][ny] = dis[x][y]+1, que.push({nx, ny});
}
}
for (auto i: vec[k]) f[i.first][i.second] = dis[i.first][i.second];
}
struct Node{int x, y, k; bool operator<(Node b)const{return k > b.k;}};
priority_queue<Node> pq;
void sol2(int k){
memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis));
for (auto i: vec[k-1]) pq.push({i.first, i.second, dis[i.first][i.second]=f[i.first][i.second]});
while (pq.size()){
int x(pq.top().x), y(pq.top().y); pq.pop(); if (vis[x][y]) continue; vis[x][y] = 1;
for (int i(0), nx, ny; i<4; ++i){
nx = x+movex[i]; ny = y+movey[i];
if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && dis[nx][ny] > dis[x][y]+1) pq.push({nx, ny, dis[nx][ny]=dis[x][y]+1});
}
}
for (auto i: vec[k]) f[i.first][i.second] = dis[i.first][i.second];
}
两者有什么区别?
完整代码: