2026-4-26 题目总结
题目总结
T2 - Fair
题意概括
有
思路
首先,我们不用动什么脑子就可以想到,我们可以枚举每个城市 bfs,算出它到城镇 bfs, 肯定会
所以,我们需要进行优化。
我们可以先从数据范围入手,我们发现,虽然 bfs,一般不会超时。
这里,我们可以引入一个新的词语—— 多源性 bfs:
顾名思义,它的意思就是有多个起点的
bfs。别把这个想的太复杂了,它其实除了开始时多推入点节点,其他和普通bfs没什么区别。
所以,我们可以不对每个城市进行一次 bfs,直接找出所有生产城镇的 bfs,最后记录到每个城市的距离,累加前
复杂度
-
时间复杂度:
O(k \times (n + m) + nk \log_2 n) ,bfsk \times (n + m) ,排序nk \log_2 n 。 -
空间复杂度:
O(n \times k + n + m)
参考代码
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
vector<int> g[N];
int a[N],dis[N][110];
void bfs(int x,int n){
queue<int> q;
for(int i = 1;i <= n;i++){
if(a[i] == x){
q.push(i);
dis[i][x] = 0;
} else{
dis[i][x] = -1;
}
}
while(!q.empty()){
int now = q.front();
q.pop();
for(auto v : g[now]){
if(dis[v][x] == -1){
dis[v][x] = dis[now][x] + 1;
q.push(v);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m,k,s;
cin >> n >> m >> k >> s;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= m;i++){
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1;i <= k;i++){
bfs(i,n);
}
for(int i = 1;i <= n;i++){
int ans = 0;
sort(dis[i] + 1,dis[i] + k + 1);
for(int j = 1;j <= s;j++){
ans += dis[i][j];
}
cout << ans << " ";
}
return 0;
}
T3 - 机器人搬重物
题意概括
在
向当前方向走
左转或右转(耗时
给定起点格点坐标、终点格点坐标以及起始朝向(东/南/西/北),求到达终点的最短时间(终点朝向任意)。无法到达则输出
思路
首先,我们发现,机器人走的并不是格子,而是格子中的点,所以,我们可以把这个机器人看成是一个
但这样就可以
所以,我们还需要在 vis 数组里添加第三个维度——决策,vis[1][1][2],代表在点 0:Creep,1:Walk,2:Run,3:Left,4:Right。
最后,就是一些细节方面:我们在尝试往前走 break,因为如果你不 break,使用 continue,你在更远的步数时就会直接判定为合法,因为偏移数组只偏移坐标,并不会检测走的格子是否为障碍。
tips: 别忘了左转和右转!
于是乎就这么写完了。。。
复杂度
- 时间复杂度:
O(N \times M) ,实际要高一些,因为决策数量时常数,会被忽略,如果要得到更精准值需\times 5 。 - 空间复杂度:
O(N \times M)
参考代码
#include <iostream>
#include <queue>
using namespace std;
const int N = 55;
int map[N][N];
bool vis[N][N][4];
int sx,sy,ex,ey,n,m;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
struct Node { int x,y,dir,step; };
int bfs(int x,int y,int dir){
queue<Node> q;
vis[x][y][dir] = true;
q.push({x,y,dir,0});
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.x == ex && now.y == ey){
return now.step;
}
for(int i = 1;i <= 3;i++){
int xx = now.x + i * dx[now.dir];
int yy = now.y + i * dy[now.dir];
if(xx < 1 || xx >= n || yy < 1 || yy >= m){
continue;
}
if(map[xx][yy] == 1){
break;
}
if(vis[xx][yy][now.dir] == true){
continue;
}
vis[xx][yy][now.dir] = true;
q.push({xx,yy,now.dir,now.step + 1});
}
int ndir1 = (now.dir + 1) % 4;
if(vis[now.x][now.y][ndir1] == false){
vis[now.x][now.y][ndir1] = true;
q.push({now.x,now.y,ndir1,now.step + 1});
}
int ndir2 = (now.dir + 3) % 4;
if(vis[now.x][now.y][ndir2] == false){
vis[now.x][now.y][ndir2] = true;
q.push({now.x,now.y,ndir2,now.step + 1});
}
}
return -1;
}
int main(){
cin >> n >> m;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
bool x;
cin >> x;
if(x){
map[i][j] = 1;
map[i][j - 1] = 1;
map[i - 1][j] = 1;
map[i - 1][j - 1] = 1;
}
}
}
char op;
cin >> sx >> sy >> ex >> ey >> op;
int dir;
if(op == 'E'){
dir = 0;
} else if(op == 'S'){
dir = 1;
} else if(op == 'W'){
dir = 2;
} else{
dir = 3;
}
cout << bfs(sx,sy,dir);
return 0;
}