[Codevs] 一塔湖图
http://codevs.cn/problem/1024/
floyd 走起
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define inf 0x7ffffff #define gc getchar() int x[10], y[10], n, m, t, k, x1, y1, x2, y2, a, b, f[120][120]; inline int read(){ int x = 0; char c = gc; while(c < '0' || c > '9') c = gc; while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc; return x; } int main() { n = read(); m = read(); t = read(); k = read(); for(int i = 1; i <= n; i ++) x[i] = read(); for(int i = 1; i <= m; i ++) y[i] = read(); for(int i = 1; i <= n * m; i ++) for(int j = 1; j <= n * m; j ++) if(i == j) f[i][j] = 0; else f[i][j] = inf; for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++){ if(i > 1) {a = i - 1; b = j; f[(j - 1) * n + i][(b - 1) * n + a] = x[i] - x[i - 1];} if(j > 1) {a = i; b = j - 1; f[(j - 1) * n + i][(b - 1) * n + a] = y[j] - y[j - 1];} if(i < n) {a = i + 1; b = j; f[(j - 1) * n + i][(b - 1) * n + a] = x[i + 1] - x[i];} if(j < m) {a = i; b = j + 1; f[(j - 1) * n + i][(b - 1) * n + a] = y[j + 1] - y[j];} } for(int i = 1; i <= t; i ++){ x1 = read(); y1 = read(); x2 = read(); y2 = read(); f[(y1 - 1) * n + x1][(y2 - 1) * n + x2] = inf; f[(y2 - 1) * n + x2][(y1 - 1) * n + x1] = inf; } for(int i = 1; i <= k; i ++) { x1 = read(); x2 = read(); y1 = read(); y2 = read(); for(a = x1; a <= x2 - 1; a ++) for(b = y1 + 1; b <= y2 - 1; b ++) { f[(b - 1) * n + a][(b - 1) * n + a + 1] = inf; f[(b - 1) * n + a + 1][(b - 1) * n + a] = inf; } for(b = y1; b <= y2 - 1; b ++) for( a = x1 + 1; a <= x2 -1; a ++) { f[(b - 1) * n + a][b * n + a] = inf; f[b * n + a][(b - 1) * n + a] = inf; } } for(int k = 1; k <= n * m; k ++) for(int i = 1; i <= n * m; i ++) for(int j = 1; j <= n * m; j ++) f[i][j] = min(f[i][k] + f[k][j], f[i][j]); x1 = read(); y1 = read(); x2 = read(); y2 = read(); printf("%d", f[(y1 - 1) * n + x1][(y2 - 1) * n + x2]); return 0; }