[JZOJ5459]【NOIP2017提高A组冲刺11.7】密室
话说这题很眼熟啊...
直接状压一发,然后跑一个bfs。
其实是码农题, 此题一A没有调试真的是特别爽。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; inline int read() { int res=0;char c=getchar();bool f=0; while(!isdigit(c)) {if(c=='-')f=1;c=getchar();} while(isdigit(c))res=(res<<3)+(res<<1)+(c^48),c=getchar(); return f?-res:res; } int n, m, k; struct edge{ int nxt, to, sit; }ed[6006]; int head[5005], cnt; inline void add(int x, int y, int z) { ed[++cnt] = (edge){head[x], y, z, }; head[x] = cnt; } int w[5005]; struct date { int x, sit; }; int dis[1<<11][5005]; int main() { freopen("room.in", "r", stdin); freopen("room.out", "w", stdout); n = read(), m = read(), k = read(); for (int i = 1; i <= n; i ++) { int x = 0; for (int j = 1 ; j <= k ; j ++) { x <<= 1; x |= read(); } w[i] = x; } for (int i = 1 ; i <= m ; i ++) { int x = read(), y = read(); int re = 0; for (int j = 1 ; j <= k ; j ++) { re <<= 1; re |= read(); } add(x, y, re); } queue <date> q; memset(dis, -1, sizeof dis); q.push((date){1, w[1]}); dis[w[1]][1] = 0; while(!q.empty()) { int x = q.front().x, so = q.front().sit; q.pop(); for (int i = head[x] ; i ; i = ed[i].nxt) { int to = ed[i].to; if ((so & ed[i].sit) == ed[i].sit and dis[so|w[to]][to] == -1) { dis[so|w[to]][to] = dis[so][x] + 1; q.push((date){to, so | w[to]}); } } } int ans = 1e9; for (int i = 0 ; i <= (1 << k) - 1 ; i ++) if (dis[i][n] != -1)ans = min(ans, dis[i][n]); if (ans == 1e9) return puts("No Solution"), 0; printf("%d\n", ans); return 0; }