P9107题解
useful_physics · · 题解
题解仅供学习,请勿抄袭
这题是一道明显的广搜题,但是题目要求输出最先到山顶的人的用时而不是最短路径的长度,所以我们在广搜求出最短路径长度后可以进行如下处理:
如果我们只上山,不下山,那我们需要走
下山时间为
解决了算所用时间的问题,找出用时最短者和求出与用时最短者用时相同的人有多少人就很容易了。
最后给出通过代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
typedef long long LL;
int n,m,k;
struct node{
int x;
int y;
int step;
};
queue <node> q;
char ch[2005][2005];
bool visit[2005][2005];
int a[N],b[N];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
LL min(LL a,LL b){
if(a<b) return a;
else return b;
}
int bfs(){
q.push(node{1,1,0});
visit[1][1]=1;
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==n&&t.y==m){
return t.step;
}
for(int i=0;i<=3;i++){
int nx=t.x+dx[i];
int ny=t.y+dy[i];
if(nx>0&&nx<=n&&ny>0&&ny<=m&&!visit[nx][ny]&&ch[nx][ny]=='.'){
q.push(node{nx,ny,t.step+1});
visit[nx][ny]=1;
}
}
}
}
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>ch[i][j];
for(int i=1;i<=k;i++){
cin>>a[i]>>b[i];
}
LL len=bfs();
LL len2=n+m-2;
LL tmp=len-len2;
LL mn=1e18;
for(int i=1;i<=k;i++){
mn=min(mn,tmp/2*b[i]+(len2+tmp/2)*a[i]);
}
cout<<mn<<' ';
int cnt=0;
for(int i=1;i<=k;i++){
LL tmp2=tmp/2*b[i]+(len2+tmp/2)*a[i];
if(tmp2==mn) cnt++;
}
cout<<cnt;
}