萌新妹子求调
查看原帖
萌新妹子求调
511676
naoliaok_lovely楼主2024/3/1 11:10

rt,35pts35pts

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define PLI pair<LL, int>

const int N = 1.2e4 + 10, M = 5.6e4 + 10;
const LL inf = 2e9 + 10;
int n, x[N], y[N];
struct node
{
	int x1, y1, x2, y2;
}a[N];
unordered_map<LL, int> ids;
int tot;

int h[N], ne[M], e[M], w[M], idx;
void add(int a, int b, int c)
{
	e[++idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx;
}

void up(int p)
{
	int id = 0;
	for(int i = 1; i <= n; i++)
		if(a[i].x2 < x[p] && a[i].y1 <= y[p] && y[p] <= a[i].y2 && (!id || a[i].x2 > a[id].x2))
			id = i;
	if(id)
	{
		if(!ids.count(a[id].x2 * inf + y[p]))
		{
			ids[a[id].x2 * inf + y[p]] = ++tot, x[tot] = a[id].x2, y[tot] = y[p];
			add(tot, ids[a[id].x2 * inf + a[id].y1], y[p] - a[id].y1), add(ids[a[id].x2 * inf + a[id].y1], tot, y[p] - a[id].y1);
			add(tot, ids[a[id].x2 * inf + a[id].y2], a[id].y2 - y[p]), add(ids[a[id].x2 * inf + a[id].y2], tot, a[id].y2 - y[p]);
		}
		add(ids[a[id].x2 * inf + y[p]], p, x[p] - a[id].x2), add(p, ids[a[id].x2 * inf + y[p]], x[p] - a[id].x2);
	}
}
void down(int p)
{
	int id = 0;
	for(int i = 1; i <= n; i++)
		if(a[i].x1 > x[p] && a[i].y1 <= y[p] && y[p] <= a[i].y2 && (!id || a[i].x1 < a[id].x1))
			id = i;
	if(id)
	{
		if(!ids.count(a[id].x1 * inf + y[p]))
		{
			ids[a[id].x1 * inf + y[p]] = ++tot, x[tot] = a[id].x1, y[tot] = y[p];
			add(tot, ids[a[id].x1 * inf + a[id].y1], y[p] - a[id].y1), add(ids[a[id].x1 * inf + a[id].y1], tot, y[p] - a[id].y1);
			add(tot, ids[a[id].x1 * inf + a[id].y2], a[id].y2 - y[p]), add(ids[a[id].x1 * inf + a[id].y2], tot, a[id].y2 - y[p]);
		}
		add(ids[a[id].x1 * inf + y[p]], p, a[id].x1 - x[p]), add(p, ids[a[id].x1 * inf + y[p]], a[id].x1 - x[p]);
	}
}
void left(int p)
{
	int id = 0;
	for(int i = 1; i <= n; i++)
		if(a[i].y2 < y[p] && a[i].x1 <= x[p] && x[p] <= a[i].x2 && (!id || a[i].y2 > a[id].y2))
			id = i;
	if(id)
	{
		if(!ids.count(x[p] * inf + a[id].y2))
		{
			ids[x[p] * inf + a[id].y2] = ++tot, x[tot] = x[p], y[tot] = a[id].y2;
			add(tot, ids[a[id].x1 * inf + a[id].y2], x[p] - a[id].x1), add(ids[a[id].x1 * inf + a[id].y2], tot, x[p] - a[id].x1);
			add(tot, ids[a[id].x2 * inf + a[id].y2], a[id].x2 - x[p]), add(ids[a[id].x2 * inf + a[id].y2], tot, a[id].x2 - x[p]);
		}
		add(ids[x[p] * inf + a[id].y2], p, y[p] - a[id].y2), add(p, ids[x[p] * inf + a[id].y2], y[p] - a[id].y2);
	}
}
void right(int p)
{
	int id = 0;
	for(int i = 1; i <= n; i++)
		if(a[i].y1 > y[p] && a[i].x1 <= x[p] && x[p] <= a[i].x2 && (!id || a[i].y1 < a[id].y1))
			id = i;
	if(id)
	{
		if(!ids.count(x[p] * inf + a[id].y1))
		{
			ids[x[p] * inf + a[id].y1] = ++tot, x[tot] = x[p], y[tot] = a[id].y1;
			add(tot, ids[a[id].x1 * inf + a[id].y1], x[p] - a[id].x1), add(ids[a[id].x1 * inf + a[id].y1], tot, x[p] - a[id].x1);
			add(tot, ids[a[id].x2 * inf + a[id].y1], a[id].x2 - x[p]), add(ids[a[id].x2 * inf + a[id].y1], tot, a[id].x2 - x[p]);
		}
		add(ids[x[p] * inf + a[id].y1], p, a[id].y1 - y[p]), add(p, ids[x[p] * inf + a[id].y1], a[id].y1 - y[p]);
	}
}

LL dis[N];
void dijkstra(int s)
{
	memset(dis, 0x3f, sizeof(dis));
	priority_queue<PLI, vector<PLI>, greater<PLI> > q;
	q.push({dis[s] = 0, s});
	while(!q.empty())
	{
		PLI t = q.top(); q.pop();
		if(t.first != dis[t.second]) continue;
		for(int i = h[t.second]; i; i = ne[i])
		{
			int j = e[i];
			if(dis[j] > t.first + w[i])
				q.push({dis[j] = t.first + w[i], j});
		}
	}
}

int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		ids.clear(), tot = 0;
		memset(h, 0, sizeof(h)), idx = 0;
		
		cin >> a[0].x1 >> a[0].y1 >> a[0].x2 >> a[0].y2 >> n;
		for(int i = 1; i <= n; i++)
		{
			scanf("%d%d%d%d", &a[i].x1, &a[i].y1, &a[i].x2, &a[i].y2);
			if(a[i].x1 > a[i].x2) swap(a[i].x1, a[i].x2);
			if(a[i].y1 > a[i].y2) swap(a[i].y1, a[i].y2);
			
			ids[a[i].x1 * inf + a[i].y1] = ++tot, x[tot] = a[i].x1, y[tot] = a[i].y1;
			ids[a[i].x1 * inf + a[i].y2] = ++tot, x[tot] = a[i].x1, y[tot] = a[i].y2;
			ids[a[i].x2 * inf + a[i].y1] = ++tot, x[tot] = a[i].x2, y[tot] = a[i].y1;
			ids[a[i].x2 * inf + a[i].y2] = ++tot, x[tot] = a[i].x2, y[tot] = a[i].y2;
		}
		if(a[0].x1 == a[0].x2)
		{
			bool flag = 1;
			for(int i = 1; i <= n; i++)
				if(a[i].x1 < a[0].x1 && a[0].x1 < a[i].x2 && min(a[0].y1, a[0].y2) <= a[i].y1 && max(a[0].y1, a[0].y2) >= a[i].y2)
					flag = 0;
			if(flag)
			{
				cout << abs(a[0].y1 - a[0].y2) << endl;
				continue;
			}
		}
		if(a[0].y1 == a[0].y2)
		{
			bool flag = 1;
			for(int i = 1; i <= n; i++)
				if(a[i].y1 < a[0].y1 && a[0].y1 < a[i].y2 && min(a[0].x1, a[0].x2) <= a[i].x1 && max(a[0].x1, a[0].x2) >= a[i].x2)
					flag = 0;
			if(flag)
			{
				cout << abs(a[0].x1 - a[0].x2) << endl;
				continue;
			}
		}
		
		for(int i = 1; i <= n; i++)
		{
			int p1 = ids[a[i].x1 * inf + a[i].y1],
				p2 = ids[a[i].x1 * inf + a[i].y2],
				p3 = ids[a[i].x2 * inf + a[i].y1],
				p4 = ids[a[i].x2 * inf + a[i].y2];
			add(p1, p3, a[i].x2 - a[i].x1), add(p3, p1, a[i].x2 - a[i].x1);
			add(p2, p4, a[i].x2 - a[i].x1), add(p4, p2, a[i].x2 - a[i].x1);
			add(p1, p2, a[i].y2 - a[i].y1), add(p2, p1, a[i].y2 - a[i].y1);
			add(p3, p4, a[i].y2 - a[i].y1), add(p4, p3, a[i].y2 - a[i].y1);
			up(p1), left(p1), up(p2), right(p2), down(p3), left(p3), down(p4), right(p4);
		}
		
		if(!ids.count(a[0].x1 * inf + a[0].y1))
		{
			ids[a[0].x1 * inf + a[0].y1] = ++tot, x[tot] = a[0].x1, y[tot] = a[0].y1;
			int t = tot;
			up(t), down(t), left(t), right(t);
		}
		if(!ids.count(a[0].x2 * inf + a[0].y2))
		{
			ids[a[0].x2 * inf + a[0].y2] = ++tot, x[tot] = a[0].x2, y[tot] = a[0].y2;
			int t = tot;
			up(t), down(t), left(t), right(t);
		}
		
		dijkstra(ids[a[0].x1 * inf + a[0].y1]);
		
		LL ans = dis[ids[a[0].x2 * inf + a[0].y2]];
		if(ans > 1e18) puts("No Path");
		else cout << ans << endl;
	}
	return 0;
}
2024/3/1 11:10
加载中...