题目:
题解:
这三问优先级依次递减,所以只有在优先级高的答案相同的时候才去考虑更新优先级低的。这样做一遍spfa就能保证在满足优先级高的同时尽量满足优先级低的
代码:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 1e9
#define LL long long
using namespace std;
int a[35][35],dis[1000],bx,by,n,m,ex,ey,diw[1000];
int tot,nxt[8000],point[1000],v[8000],c[8000],mb;LL sol[1000];
bool vis[1000];
int cc[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,-2},{-2,-1},{-1,2},{-2,1}};
void addline(int x,int y,int z){++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; c[tot]=z;}
void spfa()
{
queue<int>q;
q.push((bx-1)*m+by);
memset(vis,0,sizeof(vis));
memset(dis,0x7f,sizeof(dis));
memset(diw,0x7f,sizeof(diw));
memset(sol,0,sizeof(sol));
diw[(bx-1)*m+by]=0;
dis[(bx-1)*m+by]=0;
sol[(bx-1)*m+by]=1;
while (!q.empty())
{
int now=q.front(); q.pop();
vis[now]=0;
for (int i=point[now];i;i=nxt[i])
if (dis[v[i]]>dis[now]+c[i])
{
diw[v[i]]=diw[now]+1;
sol[v[i]]=sol[now];
dis[v[i]]=dis[now]+c[i];
if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
}
else if (dis[v[i]]==dis[now]+c[i] && diw[v[i]]>diw[now]+1)
{
diw[v[i]]=diw[now]+1;
sol[v[i]]=sol[now];
if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
}
else if (dis[v[i]]==dis[now]+c[i] && diw[v[i]]==diw[now]+1)
{
sol[v[i]]+=sol[now];
if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
}
}
if (dis[(ex-1)*m+ey]>INF) printf("-1");
else printf("%d\n%d\n%lld",dis[(ex-1)*m+ey],diw[(ex-1)*m+ey],sol[(ex-1)*m+ey]);
}
int main()
{
int i,j,k;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if (a[i][j]==3) bx=i,by=j;
if (a[i][j]==4) ex=i,ey=j;
}
mb=(ex-1)*m+ey;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
if (a[i][j]!=2)
{
for (k=0;k<8;k++)
{
int xx=i+cc[k][0],yy=j+cc[k][1];
if (xx<1 || yy<1 || xx>n || yy>m || a[xx][yy]==2) continue;
if (a[xx][yy]==0) addline((i-1)*m+j,(xx-1)*m+yy,1);
else addline((i-1)*m+j,(xx-1)*m+yy,0);
}
}
spfa();
}