bzoj2346 & loj2632 [Baltic 2011]Lamp 最短路

题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=2346

https://loj.ac/problem/2632

题解

普及组难度的题都要想十几分钟,没救了。

对于一条边,将原来就能连起来的两个点之间建一条长度为 \(0\) 的边,翻转以后能连起来的两个点建一条长度为 \(1\) 的边。然后跑最短路就可以了。因为边权只有 \(01\),可以直接 01bfs


代码如下:

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 500 + 7;
const int M = N * N;
const int INF = 0x3f3f3f3f;

int n, m;
int q[M << 1], id[N][N], dis[M], vis[M];
char a[N][N];

struct Edge { int to, ne, w; } g[M << 2]; int head[M], tot;
inline void addedge(int x, int y, int z) { g[++tot].to = y, g[tot].w = z, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y, int z) { addedge(x, y, z), addedge(y, x, z); }

inline void bfs() {
    int hd = M, tl = M + 1;
    memset(dis, 0x3f, sizeof(dis)), q[tl] = 1, dis[1] = 0;
    while (hd < tl) {
        int x = q[++hd];
        if (vis[x]) continue;
        vis[x] = 1;
        if (x == id[n + 1][m + 1]) return;
        for fec(i, x, y) if (!vis[y] && smin(dis[y], dis[x] + g[i].w))
            if (g[i].w) q[++tl] = y;
            else q[hd--] = y;
    }
}

inline void work() {
    int tmp = 0;
    for (int i = 1; i <= n + 1; ++i)
        for (int j = 1; j <= m + 1; ++j) id[i][j] = ++tmp;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            adde(id[i][j], id[i + 1][j + 1], a[i][j] == '/');
            adde(id[i + 1][j], id[i][j + 1], a[i][j] == '\\');
        }
    bfs();
    if (dis[id[n + 1][m + 1]] != INF) printf("%d\n", dis[id[n + 1][m + 1]]);
    else puts("NO SOLUTION");
}

inline void init() {
    read(n), read(m);
    for (int i = 1; i <= n; ++i) scanf("%s", a[i] + 1);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/hankeke/p/bzoj2346.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值