BZOJ 3362 POJ 1984 Navigation Nightmare 并与正确集中检查

标题效果:一些养殖场是由一些南北或东西向的道路互连。

镶上在不断的过程中会问两个农场是什么曼哈顿的距离,假设现在是不是通信。那么输出-1。

思维:并与正确集中检查,f[i]点i至father[i]距离,为了维持两个值,一个是东西向的距离。一个是南北向的距离,由于以后更新的时候要用到。在合并的时候有些特殊。如今有一条边(x->y),设fx为x的根。fy为y的根,那么如今知道f到fx的距离。y到fy的距离。还知道x到y的距离,设fx到fy的距离为dis,则dis + f[y] = f[x] + edge[p].w,那么dis = f[x] - f[y] + edge[p].w。

依据这个公式来合并两个树就能够了。

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 40010
using namespace std;

struct Complex{
	int x,y,len;
	char c;
}edge[MAX];
struct Ask{
	int x,y;
	int pos,_id;
	bool operator <(const Ask &a)const {
		return pos < a.pos;
	}
}ask[MAX];
struct Status{
	int x,y;

	Status(int _,int __):x(_),y(__) {}
	Status() {}
	Status operator +(const Status &a)const {
		return Status(x + a.x,y + a.y);
	}
	Status operator -(const Status &a)const {
		return Status(x - a.x,y - a.y);
	}
}f[MAX];

int points,edges,asks;
int father[MAX];
int ans[MAX];

char s[10];

void Pretreatment();

int Find(int x);

int main()
{
	cin >> points >> edges;
	Pretreatment();
	for(int i = 1;i <= edges; ++i) {
		scanf("%d%d%d%s",&edge[i].x,&edge[i].y,&edge[i].len,s);
		edge[i].c = s[0];
	}
	cin >> asks;
	for(int i = 1;i <= asks; ++i)
		scanf("%d%d%d",&ask[i].x,&ask[i].y,&ask[i].pos),ask[i]._id = i;
	sort(ask + 1,ask + asks + 1);
	int now = 1;
	for(int i = 1;i <= edges; ++i) {
		int fx = Find(edge[i].x);
		int fy = Find(edge[i].y);
		if(fx != fy) {
			father[fy] = fx;
			Status temp;
			if(edge[i].c == 'N')	temp = Status(0,edge[i].len);
			if(edge[i].c == 'S')	temp = Status(0,-edge[i].len);
			if(edge[i].c == 'E')	temp = Status(edge[i].len,0);
			if(edge[i].c == 'W')	temp = Status(-edge[i].len,0);
			f[fy] = f[edge[i].x] - f[edge[i].y] + temp;
		}
		while(i >= ask[now].pos && now <= asks) {
			int fx = Find(ask[now].x);
			int fy = Find(ask[now].y);
			if(fx != fy)	ans[ask[now]._id] = -1;
			else {
				Status temp = f[ask[now].x] - f[ask[now].y];
				ans[ask[now]._id] = abs(temp.x) + abs(temp.y);
			}
			++now;
		}
	}
	for(int i = 1;i <= asks; ++i)
		printf("%d\n",ans[i]);
	return 0;
}

void Pretreatment()
{
	for(int i = 1;i <= points; ++i)
		father[i] = i;
}

int Find(int x)
{
	if(father[x] == x)	return x;
	int temp = father[x];
	father[x] = Find(father[x]);
	f[x] = f[x] + f[temp];
	return father[x];
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-11-05 16:25:01

BZOJ 3362 POJ 1984 Navigation Nightmare 并与正确集中检查的相关文章

BZOJ 3362 POJ 1984 Navigation Nightmare 带权并查集

题目大意:一些农场由一些东西向或者南北向的路相互连接.在不断加边的过程中会询问两个农场的曼哈顿距离是多少,如果目前还不连通,那么输出-1. 思路:带权并查集,f[i]为点i到father[i]的距离,要维护两个值,一个是东西向的距离,一个是南北向的距离,因为以后更新的时候要用到.在合并的时候有些特殊.现在有一条边(x->y),设fx为x的根,fy为y的根,那么现在知道f到fx的距离,y到fy的距离,还知道x到y的距离,设fx到fy的距离为dis,则dis + f[y] = f[x] + edge

POJ 1984 Navigation Nightmare 二维带权并查集

题目来源:POJ 1984 Navigation Nightmare 题意:给你一颗树 k次询问 求2点之间的曼哈顿距离 并且要在只有开始k条边的情况下 思路:按照方向 我是以左上角为根 左上角为原点 dx[i]为i点距离根的x坐标 dy[]是y坐标 这两个可以通过路径压缩求出 只不过是二维而已 #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int m

POJ 1984 Navigation Nightmare (数据结构-并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1615 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o

POJ 1984 Navigation Nightmare 【经典带权并查集】

任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7783   Accepted: 2801 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usuall

poj 1984 Navigation Nightmare(带权并查集+小小的技巧)

题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如果不存在则输出-1 其实就是将权值分一下x,y,x表示x轴方向的权值,y表示y轴方向的权值.然后最后询问时稍微有点技巧 可以先记录一下每次询问的位置然后再按照时间点从小到大来排序最后这样就方便并查集了. #include <iostream> #include <cstring> #i

POJ 1984 - Navigation Nightmare - [带权并查集]

题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000)

BZOJ 3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦

Description 给你每个点与相邻点的距离和方向,求两点间的曼哈顿距离. \(n \leqslant 4\times 10^4\) . Sol 加权并查集. 像向量合成一样合并就可以了,找 \(f[x]\) 的时候需要先记录现在的父节点,然后更新他新的父节点. Code /************************************************************** Problem: 3362 User: BeiYu Language: C++ Result:

poj 1984 Navigation Nightmare 并查集 解题报告

Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 1000) connect the farms.

【POJ 1984】Navigation Nightmare(带权并查集)

Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 100