求调
查看原帖
求调
1051166
Nazq楼主2024/3/3 17:26
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 40005; //20000*2?行和列
const int M = 200005; //100000*2一个点,两条有向边
const int Inf = 0x3f3f3f3f+1;
struct Edge {
	int to, next;
} e[M];
struct Point {
	int x, y, dis;
	bool operator< (const Point& that)const {
		return this->dis > that.dis;
	}
};
int n, m, sx, sy, tx, ty, len, head[N], dis[N], ans = Inf;
bool vis[N];
void Insert(const int& u, const int& v) {
	e[++len] = {v, head[u]};
	head[u] = len;
}
void Dijsktra() { //O(mlogn)
	priority_queue<Point> q;
	dis[sx] = 0;
	dis[sy] = 0;
	q.push({sx, sy, 0});
	while (!q.empty()) {
		int x = q.top().x, y = q.top().y;
		q.pop();
		if (!vis[x]) {
			vis[x] = true;
			if (x == tx)
				ans = min(ans, dis[x] + (abs(y - ty) << 1));
			for (int i = head[x], v, w; i; i = e[i].next) {
				v = e[i].to;
				w = (abs(y - v) << 1) + 1;
				if (dis[v] > dis[x] + w) {
					dis[v] = dis[x] + w;
					q.push({x, v, dis[v]});
				}
			}
		}
		if (!vis[y]) {
			vis[y] = true;
			if (y == ty)
				ans = min(ans, dis[y] + (abs(x - tx) << 1));
			for (int i = head[y], v, w; i; i = e[i].next) {
				v = e[i].to;
				w = (abs(x - v) << 1) + 1;
				if (dis[v] > dis[y] + w) {
					dis[v] = dis[y] + w;
					q.push({v, y, dis[v]});
				}
			}
		}
	}
}
int main() {
	scanf("%d%d", &n, &m);
	fill(dis + 1, dis + n + n + 1, Inf);
	for (int i = 1, x, y; i <= m; ++i) {
		scanf("%d%d", &x, &y);
		Insert(x, y + n);
		Insert(y + n, x);
	}
	scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
	sy += n;
	ty += n;
	Dijsktra();
	if (ans == Inf)
		ans = -1;
	printf("%d\n", ans);
	return 0;
}

WA了5 7 10三个点

2024/3/3 17:26
加载中...