求问多源最短路
  • 板块学术版
  • 楼主rainygame
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/10/24 17:24
  • 上次更新2024/10/24 17:28:14
查看原帖
求问多源最短路
804607
rainygame楼主2024/10/24 17:24
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];
}

两者有什么区别?

完整代码:

2024/10/24 17:24
加载中...