【SPOJ-DESRUG】Desrugenstein【Floyd】

题意:

给出一个NxN的网格,给出每个网格可以走的方向,有一些询问,问从一点到另一点的最短路长度。


多源最短路裸题,但是读入太恶心了,而且表述不清楚...


附自己的画的样例的图:

(比较丑)



#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 105, inf = 0x3f3f3f3f;

int n, g[maxn][maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int getid(int x, int y) {
	return y * n + x;
}

int main() {
	while(1) {
		n = iread();
		if(n == 0) break;

		int tot = n * n;
		for(int i = 0; i < tot; i++) for(int j = 0; j < tot; j++) g[i][j] = (i != j) * inf;

		for(int i = n - 1; i >= 0; i--) for(int j = 0; j < n; j++) {
			int a = iread(), b = iread(), c = iread(), d = iread();
			if(a == 1) g[getid(j, i)][getid(j, i + 1)] = 1;
			if(b == 1) g[getid(j, i)][getid(j, i - 1)] = 1;
			if(c == 1) g[getid(j, i)][getid(j - 1, i)] = 1;
			if(d == 1) g[getid(j, i)][getid(j + 1, i)] = 1;
		}

		for(int k = 0; k < tot; k++) for(int i = 0; i < tot; i++) for(int j = 0; j < tot; j++)
			g[i][j] = min(g[i][j], g[i][k] + g[k][j]);

		int m = iread();
		while(m--) {
			int x0 = iread(), y0 = iread(), x1 = iread(), y1 = iread(), ans = g[getid(x0, y0)][getid(x1, y1)];
			ans != inf ? printf("%d\n", ans) : printf("Impossible\n");
		}
	}

	return 0;
}


你可能感兴趣的:(floyd,多源最短路)