poj1984(并查集)

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. A map of these farms might look something like the illustration below in which farms are labeled F1..F7 for clarity and lengths between connected farms are shown as (n):
           F1 --- (13) ---- F6 --- (9) ----- F3

            |                                 |

           (3)                                |

            |                                (7)

           F4 --- (20) -------- F2            |

            |                                 |

           (2)                               F5

            | 

           F7 

Being an ASCII diagram, it is not precisely to scale, of course.

Each farm can connect directly to at most four other farms via roads that lead exactly north, south, east, and/or west. Moreover, farms are only located at the endpoints of roads, and some farm can be found at every endpoint of every road. No two roads cross, and precisely one path
(sequence of roads) links every pair of farms.

FJ lost his paper copy of the farm map and he wants to reconstruct it from backup information on his computer. This data contains lines like the following, one for every road:

There is a road of length 10 running north from Farm #23 to Farm #17
There is a road of length 7 running east from Farm #1 to Farm #17
...

As FJ is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob:

What is the Manhattan distance between farms #1 and #23?

FJ answers Bob, when he can (sometimes he doesn't yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the "Manhattan" distance between the pair of farms.
The Manhattan distance between two points (x1,y1) and (x2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab in a large city must travel over city streets in a perfect grid to connect two x,y points).

When Bob asks about a particular pair of farms, FJ might not yet have enough information to deduce the distance between them; in this case, FJ apologizes profusely and replies with "-1".

Input

* Line 1: Two space-separated integers: N and M



* Lines 2..M+1: Each line contains four space-separated entities, F1,

        F2, L, and D that describe a road. F1 and F2 are numbers of

        two farms connected by a road, L is its length, and D is a

        character that is either 'N', 'E', 'S', or 'W' giving the

        direction of the road from F1 to F2.



* Line M+2: A single integer, K (1 <= K <= 10,000), the number of FB's

        queries



* Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob

        and contains three space-separated integers: F1, F2, and I. F1

        and F2 are numbers of the two farms in the query and I is the

        index (1 <= I <= M) in the data after which Bob asks the

        query. Data index 1 is on line 2 of the input data, and so on.

Output

* Lines 1..K: One integer per line, the response to each of Bob's

        queries.  Each line should contain either a distance

        measurement or -1, if it is impossible to determine the

        appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6 1
1 4 3
2 6 6

Sample Output

13
-1
10

Hint

At time 1, FJ knows the distance between 1 and 6 is 13.
At time 3, the distance between 1 and 4 is still unknown.
At the end, location 6 is 3 units west and 7 north of 2, so the distance is 10.

Source

USACO 2004 February
 
此题是求哈夫曼距离。首先得判断连通性,若所求两点不连通,则哈夫曼距离为-1;若联通,则哈夫曼距离为|x1-x2| + |y1-y2|
若两点不连通,则构建各自父亲节点间的关系。当求两节点哈夫曼距离时可以通过各自与公共父亲节点的关系过度。
注意首先分别在x与y方向分别定义正方向,便于作家兼运算。
 
注意询问的顺序可能颠倒,即先询问的图构建程度比很面的还好,即前一次询问图可能要求构建到第k+1步,二本次询问只要求图构建到第k步,若不事先对构建的步数下标进行排序,可能会出错。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int MAX=40000+10;
int par[MAX];
int dx[MAX];
int dy[MAX];
struct node1
{
	int st,en,dis;
	char dir[5];
}road[MAX];
struct node2
{
	int s,e,in,ans,seq;
}query[MAX];

bool cmp1(node2 a,node2 b)
{
	return a.in<b.in;
}

bool cmp2(node2 a,node2 b)
{
	return a.seq<b.seq;
}

int Get_par(int a)
//查找a的父亲节点并压缩路径
{
	if(par[a]==a)
		return par[a];
	//注意语句的顺序
	int pa=par[a];
	par[a]=Get_par(par[a]);

	dx[a]+=dx[pa],dy[a]+=dy[pa];///
	return par[a];
}


void Merge1(int a,int b,int dis)
//合并a,b
{
	int pa,pb;
	pa=Get_par(a);
	pb=Get_par(b);
	
	dx[pa]=-dx[a]+dis+dx[b];//
	dy[pa]=dy[b]-dy[a];

	par[pa]=pb;
}

void Merge2(int a,int b,int dis)
//合并a,b
{
	int pa,pb;
	pa=Get_par(a);
	pb=Get_par(b);

	dy[pa]=-dy[a]+dis+dy[b];//
	dx[pa]=dx[b]-dx[a];

	par[pa]=pb;
}

int main()
{
	int i,m,k,n,ind,j;
	char ch;
	while(~scanf("%d%d",&n,&m))
	{
		for(i=1;i<=n;i++)
			par[i]=i,dx[i]=0,dy[i]=0;

		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d%s",&road[i].st,&road[i].en,&road[i].dis,road[i].dir);

		//	printf("%d  %d  %d  %c\n",road[i].st,road[i].en,road[i].dis,road[i].dir);
		}

		scanf("%d",&k);
		for(i=1;i<=k;i++)
		{
			scanf("%d%d%d",&query[i].s,&query[i].e,&query[i].in);
			query[i].seq=i;
		}
		sort(query+1,query+k+1,cmp1);

		j=1;
		for(i=1;i<=k;i++)
		{
			while(j<=m&&j<=query[i].in)
			{
				int pss=Get_par(road[j].st);
		        int pee=Get_par(road[j].en);
				if(pss!=pee)
				{
				 if(road[j].dir[0]=='E')
		         	Merge1(road[j].st,road[j].en,road[j].dis);
			     else if(road[j].dir[0]=='W')
				    Merge1(road[j].st,road[j].en,-road[j].dis);
			     else if(road[j].dir[0]=='N')
			     	Merge2(road[j].st,road[j].en,road[j].dis);
		       	else if(road[j].dir[0]=='S')
				Merge2(road[j].st,road[j].en,-road[j].dis);
				}
				j++;
			}
			int ps=Get_par(query[i].s);
			int pe=Get_par(query[i].e);
			if(ps!=pe)
				query[i].ans=-1;
			else
			{
				int disx,disy;
				disx=abs(dx[query[i].s]-dx[query[i].e]);
				disy=abs(dy[query[i].s]-dy[query[i].e]);
				query[i].ans=disx+disy;
			}
		}

		sort(query+1,query+k+1,cmp2);
		for(i=1;i<=k;i++)
			printf("%d\n",query[i].ans);

	}
	return 0;
}

 
 

你可能感兴趣的:(数据结构,并查集)