[ZJOI2016]旅行者 整体二分+dijsktra

Description
给你一个n*m网状的图,每个点可以达到他上下左右四个点。
给你q个询问,每次询问任意两点间的距离。


Sample Input
2 2
2
3
6 4
2
1 1 2 2
1 2 2 1


Sample Output
6
7


这道题整体二分啊,太强了。。。
对于我们每次取一条分割线,假设出发点和起始点再这条直线的两边,那它们之间的最短路径肯定经过这条分割线上的某个节点,你就对于这条分割线上的每个节点做一次dij,然后去更新答案,如果在同一边的就继续二分下去。
你可以每次选择一个比较大的坐标轴做分割线,这样时间复杂度可以到O(nlog n sqrt n)。


#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;
int _min(int x, int y) {return x < y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct node {
	int x, d;
	friend bool operator < (node a, node b) {return a.d > b.d;}
}; priority_queue<node> qq;
struct edge {
	int x, y, c, next;
} e[81000]; int len, last[21000];
struct query {
	int x1, y1, x2, y2, opt;
} q[110000], g1[110000], g2[110000]; int ans[110000];
int uu, d[21000], v[21000], gg[21000];
int n, m;

void ins(int x, int y, int c) {
	e[++len].x = x; e[len].y = y; e[len].c = c;
	e[len].next = last[x]; last[x] = len;
}

bool check(int now, int x1, int y1, int x2, int y2) {
	int x = now / m; if(now % m) x++;
	int y = now % m; if(y == 0) y = m;
	if(x < x1 || x > x2 || y < y1 || y > y2) return 0;
	return 1;
}

void dij(int hx, int hy, int x1, int y1, int x2, int y2) {
	uu++;
	int st = (hx - 1) * m + hy;
	d[st] = 0; gg[st] = uu;
	node tmp; tmp.x = st, tmp.d = 0;
	qq.push(tmp);
	while(!qq.empty()) {
		node now = qq.top(); qq.pop();
		int x = now.x;
		if(v[x] == uu) continue; v[x] = uu;
		for(int k = last[x]; k; k = e[k].next) {
			int y = e[k].y;
			if(!check(y, x1, y1, x2, y2)) continue;
			if(d[y] > d[x] + e[k].c || gg[y] != uu) {
				d[y] = d[x] + e[k].c; gg[y] = uu;
				tmp.x = y, tmp.d = d[y], qq.push(tmp);
			}
		}
	}
}

void erfen(int x1, int y1, int x2, int y2, int l, int r) {
	if(l > r) return ;
	if(x1 == y1 && y1 == y2) {
		for(int i = l; i <= r; i++) ans[q[i].opt] = 0;
		return ;
	}
	if(x1 > x2 || y1 > y2) return ;
	if(y2 - y1 > x2 - x1) {
		int u = (y1 + y2) / 2;
		for(int i = x1; i <= x2; i++) {
			dij(i, u, x1, y1, x2, y2);
			for(int j = l; j <= r; j++) {
				int p1 = (q[j].x1 - 1) * m + q[j].y1, p2 = (q[j].x2 - 1) * m + q[j].y2;
				ans[q[j].opt] = _min(ans[q[j].opt], d[p1] + d[p2]);
			}
		} int l1 = 0, l2 = 0;
		for(int i = l; i <= r; i++) {
			if(q[i].y1 >= y1 && q[i].y1 < u && q[i].y2 >= y1 && q[i].y2 < u) g1[++l1] = q[i];
			else if(q[i].y1 <= y2 && q[i].y1 > u && q[i].y2 <= y2 && q[i].y2 > u) g2[++l2] = q[i];
		} 
		for(int i = 1; i <= l1; i++) q[l + i - 1] = g1[i];
		for(int i = 1; i <= l2; i++) q[l + l1 + i - 1] = g2[i];
		erfen(x1, y1, x2, u - 1, l, l + l1 - 1), erfen(x1, u + 1, x2, y2, l + l1, l + l1 + l2 - 1);
	} else {
		int u = (x1 + x2) / 2;
		for(int i = y1; i <= y2; i++) {
			dij(u, i, x1, y1, x2, y2);
			for(int j = l; j <= r; j++) {
				int p1 = (q[j].x1 - 1) * m + q[j].y1, p2 = (q[j].x2 - 1) * m + q[j].y2;
				ans[q[j].opt] = _min(ans[q[j].opt], d[p1] + d[p2]);
			}
		} int l1 = 0, l2 = 0;
		for(int i = l; i <= r; i++) {
			if(q[i].x1 >= x1 && q[i].x1 < u && q[i].x2 >= x1 && q[i].x2 < u) g1[++l1] = q[i];
			else if(q[i].x1 <= x2 && q[i].x1 > u && q[i].x2 <= x2 && q[i].x2 > u) g2[++l2] = q[i];
		}
		for(int i = 1; i <= l1; i++) q[l + i - 1] = g1[i];
		for(int i = 1; i <= l2; i++) q[l + l1 + i - 1] = g2[i];
		erfen(x1, y1, u - 1, y2, l, l + l1 - 1), erfen(u + 1, y1, x2, y2, l + l1, l + l1 + l2 - 1);
	}
}

int main() {
	n = read(), m = read();
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j < m; j++) {
			int x = read();
			ins((i - 1) * m + j, (i - 1) * m + j + 1, x);
			ins((i - 1) * m + j + 1, (i - 1) * m + j, x);
		}
	}
	for(int i = 1; i < n; i++) {
		for(int j = 1; j <= m; j++) {
			int x = read();
			ins((i - 1) * m + j, i * m + j, x);
			ins(i * m + j, (i - 1) * m + j, x);
		}
	} int Q = read();
	for(int i = 1; i <= Q; i++) q[i].x1 = read(), q[i].y1 = read(), q[i].x2 = read(), q[i].y2 = read(), q[i].opt = i;
	memset(ans, 63, sizeof(ans));
	erfen(1, 1, n, m, 1, Q);
	for(int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值