版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SSL_hzb/article/details/82823156
题意
有 个房间, 个传送门, 种钥匙,每个房间有一些钥匙,每个传送门可以从一个房间传到另一个房间,需要一定的钥匙才能使用传送门,求出经过最少的传送门能从 号房间到达 号房间。
思路
压缩钥匙的状态,然后 就行了。
代码
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
int to, k, next;
}e[6001];
struct state{
int w, k;
};
int N, M, K, ans, x;
int key[5001], list[5001], d[2048][5001];
void bfs() {
queue<state> q;
q.push((state){1, key[1]});
memset(d, -1, sizeof(d));
d[key[1]][1] = 0;
while (q.size()) {
state head = q.front();
q.pop();
for (int i = list[head.w]; i; i = e[i].next) {
int s = head.k, y = e[i].to;
if ((s & e[i].k) == e[i].k && d[s | key[y]][y] == -1) {//判断分层
q.push((state){y, s | key[y]});
d[s | key[y]][y] = d[s][head.w] + 1;
}
}
if (head.w == N) ans = min(ans, d[head.k][N]);
}
}
int main() {
scanf("%d %d %d", &N, &M, &K);
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= K; j++) {
scanf("%d", &x);
key[i] = key[i] << 1 | x;
}
}
for (int i = 1; i <= M; i++) {
scanf("%d %d", &x, &e[i].to);
e[i].next = list[x];
list[x] = i;
for (int j = 1; j <= K; j++) {
scanf("%d", &x);
e[i].k = e[i].k << 1 | x;
}
}
ans = 2147483647;
bfs();
if (ans == 2147483647) printf("No Solution");
else printf("%d", ans);
}