zoukankan      html  css  js  c++  java
  • 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
    ", 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;
    }
    
  • 相关阅读:
    Python基础:条件判断与循环的两个要点
    oozie note
    Dynamics CRM2013 Server2012R2下IFD部署遇到There is already a listener on IP endpoint的解决方法
    监控系统的多协议直播(RTSP RTMP HTTP Live Streaming)
    易迅,生的霸气,死的窝囊
    js中的splice方法和slice方法简单总结
    JAVA虚拟机的安装以及JAVA的环境配置
    JS函数种类详解
    IDEA多线程调试设置
    Java中的HashMap源码记录以及并发环境的几个问题
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj2346.html
Copyright © 2011-2022 走看看