【LOJ6121】【网络流24题】孤岛营救问题(SPFA)

Description

https://loj.ac/problem/6121


Solution

dis[x][y][s] d i s [ x ] [ y ] [ s ] 表示位于 (x,y) ( x , y ) 、当前得到的钥匙状态为 s s 的最短路,SPFA即可。


Code

/**************************************
 * Au: Hany01
 * Prob: [LOJ6121][网络流24题]孤岛营救问题
 * Date: Jul 18th, 2018
 * Email: [email protected]
**************************************/

#include

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define x first
#define y second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    register char c_; register int _, __;
    for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 11, maxst = 1 << 10;

int dt[4][2] = {1, 0, 0, 1, -1, 0, 0, -1}, dirw[3][3];
int n, m, ban[maxn][maxn][4], vis[maxn][maxn][maxst], dis[maxn][maxn][maxst], p, key[maxn][maxn], Ans;

int main()
{
#ifdef hany01
    File("loj6121");
#endif

    static int x1, y1, x2, y2;
    static queueint> > q;

    dirw[2][1] = 0, dirw[1][2] = 1, dirw[0][1] = 2, dirw[1][0] = 3;

    n = read(), m = read(), p = read();
    for (static int k = read(), t; k --; ) {
        x1 = read(), y1 = read(), x2 = read(), y2 = read(), t = read();
        if (!t) t = -1;
        ban[x1][y1][dirw[x2 - x1 + 1][y2 - y1 + 1]] = t, ban[x2][y2][dirw[x1 - x2 + 1][y1 - y2 + 1]] = t;
    }
    for (static int s = read(); s --; )
        x1 = read(), y1 = read(), key[x1][y1] |= (1 << (read() - 1));

    for (q.push(mp(mp(1, 1), key[1][1])), Set(dis, 127), dis[1][1][key[1][1]] = 0; !q.empty(); ) {
        int x = q.front().x.x, y = q.front().x.y, st = q.front().y, nx, ny, nst;
        q.pop(), vis[x][y][st] = 0;
        rep(i, 4) {
            if (ban[x][y][i] < 0 || (ban[x][y][i] && !(st & (1 << (ban[x][y][i] - 1))))) continue;
            nx = x + dt[i][0], ny = y + dt[i][1], nst = st | key[nx][ny];
            if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
            if (chkmin(dis[nx][ny][nst], dis[x][y][st] + 1))
                if (!vis[nx][ny][nst]) vis[nx][ny][nst] = 1, q.push(mp(mp(nx, ny), nst));
        }
    }
    Ans = 0x7f7f7f7f;
    rep(i, 1 << p) chkmin(Ans, dis[n][m][i]);
    if (Ans == 0x7f7f7f7f) puts("-1"); else printf("%d\n", Ans);

    return 0;
}

你可能感兴趣的:(LOJ,网络流,SPFA)