analysis
分層圖最短路+01BFS
這裡不需要建圖,圖在心中
code
#include<bits/stdc++.h>
using namespace std;
#define loop(i,start,end) for(register int i=start;i<=end;++i)
#define clean(arry,num) memset(arry,num,sizeof(arry))
#define ll long long
#define pos(x,y) (((x-1)*m+y))
template<typename T>void rd(T &x){
x=0;char r=getchar();T neg=1;
while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
while(r>='0'&&r<='9'){x=(x<<1)+(x<<3)+r-'0';r=getchar();}
x*=neg;
}
const int maxn=10+10,maxm=10+10,maxp=10+10,maxk=200;
int n,m,p,k,s;
int a[maxn][maxn][maxn][maxn];//easier
int key[maxn][maxn][maxp];
int nfk[maxn][maxn];
struct node{
int x;
int y;
int step;
int state;
node():x(0),y(0),step(0),state(0){}
node(int x,int y,int step,int state):x(x),y(y),step(step),state(state){}
};
queue<node>q;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
bool vis[maxn][maxn][(1<<(maxp-10))+10];
void bfs(){
q.push(node(1,1,0,0));
while(q.empty()==false){
node f=q.front();
q.pop();
if(f.x==n&&f.y==m){
printf("%d\n",f.step);
return;
}
loop(i,0,3){
int nx=f.x+dx[i];
int ny=f.y+dy[i];
if(nx*ny==0||nx>n||ny>m)
continue;
int nstate=f.state;
if(a[f.x][f.y][nx][ny]==-1||(a[f.x][f.y][nx][ny]>0&&(f.state&(1<<(a[f.x][f.y][nx][ny]-1))))){
loop(i,1,nfk[nx][ny])
nstate|=(1<<(key[nx][ny][i]-1));
if(!vis[nx][ny][nstate]){
q.push(node(nx,ny,f.step+1,nstate));
vis[nx][ny][nstate]=true;
}
}
}
}
printf("-1\n");
}
int main(){
#ifndef ONLINE_JUDGE
freopen("datain.txt","r",stdin);
#endif
clean(key,0);
clean(a,-1);
clean(nfk,0);
clean(vis,false);
rd(n);
rd(m);
rd(p);
rd(k);
loop(i,1,k){
int xi,yi,xj,yj,gi;
rd(xi);
rd(yi);
rd(xj);
rd(yj);
rd(gi);
a[xi][yi][xj][yj]=gi;
a[xj][yj][xi][yi]=gi;
}
rd(s);
loop(i,1,s){
int xi,yi,qi;
rd(xi);
rd(yi);
rd(qi);
key[xi][yi][++nfk[xi][yi]]=qi;
}
bfs();
return 0;
}