作者:凯锐斯_372 | 来源:互联网 | 2023-01-17 14:19
题目地址:https:vjudge.netcontest65959#problemN思路:两个人,在多个KFC中找总路程最短的。我已开始是对于每个终点跑一边bfs,然后tle
题目地址:https://vjudge.net/contest/65959#problem/N
思路:两个人,在多个KFC中找总路程最短的。我已开始是对于每个终点跑一边bfs,然后tle了……后来对于每个人单独跑一边,然后记录到每个点的最短距离并相加。然后对于两个人都能走到的终点找一下最小的距离。
AC代码:
#include
using namespace std;
const int maxn=200+10;
char g[maxn][maxn];
bool vis[maxn][maxn];
int ans[maxn];
int temp[maxn];
int sum;
struct pos{
int x,y,s;
};
int n,m;
pos Y,M;
vectorKCF;
int xx[4]={-1,1,0,0};
int yy[4]={0,0,1,-1};
void init()
{
KCF.clear();
memset(vis,false,sizeof(vis));
memset(g,0,sizeof(g));
}
void bfs()
{
memset(ans,0,sizeof(ans));
memset(temp,0,sizeof(temp));
memset(vis,false,sizeof(vis));
queueq;
q.push(Y);
vis[Y.x][Y.y]=true;
while(!q.empty())
{
pos now=q.front();
q.pop();
for(int i=0;i=n ||tempy>=m)
continue;
if(vis[tempx][tempy] || g[tempx][tempy]=='#')
continue;
vis[tempx][tempy]=true;
q.push(pos{tempx,tempy,now.s+1});
}
}
while(!q.empty())
q.pop();
q.push(M);
vis[M.x][M.y]=true;
memset(vis,false,sizeof(vis));
while(!q.empty())
{
pos now=q.front();
q.pop();
for(int i=0;i=n ||tempy>=m)
continue;
if(vis[tempx][tempy] || g[tempx][tempy]=='#')
continue;
vis[tempx][tempy]=true;
q.push(pos{tempx,tempy,now.s+1});
}
}
int min=0x3f3f3f3f;
for(int i=0;i