1. 程式人生 > >POJ 1984 - Navigation Nightmare - [帶權並查集]

POJ 1984 - Navigation Nightmare - [帶權並查集]

locate The scan nav memory ads pre unknown farm

題目鏈接: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) 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.

題意:

有n個節點,給出m條數據,每條數據包含F1,F2,L,D,代表從節點F1到F2的距離為L,D為‘E/W/S/N‘,代表了F1→F2是指向東/西/南/北;

現在又有個人來給出k條詢問,每條詢問包含F1,F2,IDX,代表了查詢節點F1和F2之間的距離,本次查詢發生在錄入第IDX條數據之後(也就是說本次查詢時,第IDX+1條往後的數據都還是未知的);

註意:對於查詢的回答,必須按照查詢輸入的順序進行輸出;同時可能在讀入第idx條數據之後,讀入第idx+1條數據之前,會有多個查詢。

題解:

並查集建樹,par[x]代表x的父親節點,val[x].X和val[x].Y分別代表par[x]->x向量的水平分量和豎直分量;

註意做好find()函數內val[x]的更新、unite兩個節點時val[]更新,並且註意將答案按照查詢的順序輸出即可。

AC代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=40000+10;

int n,m,k;

int par[maxn];
struct Val{
    int X,Y; //par[x]->x向量的水平分量和豎直分量
}val[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i,val[i].X=val[i].Y=0;}
int find(int x)
{
    if(par[x]==x) return x;
    else
    {
        int root=find(par[x]);
        val[x].X+=val[par[x]].X;
        val[x].Y+=val[par[x]].Y;
        return par[x]=root;
    }
}

struct Data{
    int F1,F2,L;
    char D[3];
}data[maxn];

vector<int> D2Q[maxn]; //Data->Query
struct Query{
    int F1,F2;
    int id; //記錄下是第id個查詢
}query[maxn];

struct Res{
    int val; //第id個查詢的答案值
    int id; //代表本結果是對應到第id個查詢的
    Res(int val,int id){this->val=val,this->id=id;}
    bool operator<(const Res &oth)const
    {
        return id<oth.id;
    }
};

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d%d%d%s",&data[i].F1,&data[i].F2,&data[i].L,data[i].D);

    scanf("%d",&k);
    for(int i=1;i<=n;i++) D2Q[i].clear();
    for(int i=1,idx;i<=k;i++)
    {
        scanf("%d%d%d",&query[i].F1,&query[i].F2,&idx);
        query[i].id=i;
        D2Q[idx].push_back(query[i].id); //記錄一下第i個詢問發生在第idx個數據之後
    }

    init(1,n);
    vector<Res> res;
    for(int i=1,a,b,t1,t2;i<=m;i++)
    {
        a=data[i].F1, b=data[i].F2;
        t1=find(a), t2=find(b);
        if(t1!=t2)
        {
            par[t2]=t1;
            int dX,dY; //dX是a->b向量的水平分量,dY是a->b向量的豎直分量
            if(data[i].D[0]==E) dX=data[i].L, dY=0;
            if(data[i].D[0]==W) dX=-data[i].L, dY=0;
            if(data[i].D[0]==N) dX=0, dY=data[i].L;
            if(data[i].D[0]==S) dX=0, dY=-data[i].L;
            val[t2].X=val[a].X+dX-val[b].X;
            val[t2].Y=val[a].Y+dY-val[b].Y;
        }

        //在錄入本次數據之後,查看是否有查詢,若有嘗試進行回答
        for(int j=0,_size=D2Q[i].size();j<_size;j++)
        {
            Query Q=query[D2Q[i][j]];
            a=Q.F1, b=Q.F2;
            t1=find(a), t2=find(b);

            int ans;
            if(t1!=t2) ans=-1;
            else ans=abs(val[a].X-val[b].X)+abs(val[a].Y-val[b].Y);

            res.push_back(Res(ans,Q.id)); //將查詢結果進行記錄
        }
    }

    sort(res.begin(),res.end()); //將查詢的結果按照之前查詢的順序排列
    for(int i=0;i<res.size();i++) printf("%d\n",res[i].val);
}

POJ 1984 - Navigation Nightmare - [帶權並查集]