POJ 1984 Navigation Nightmare(帶權並查集)

Navigation Nightmarenode

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 8001   Accepted: 2883
Case Time Limit: 1000MS

Descriptionios

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): app

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". 函數

Inputthis

* 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.

Outputspa

* 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.net

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 Outputcode

13
-1
10

Hintorm

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. htm

題意:

給你n(n<=4e4)個點,m(m<4e4)條邊,每條邊(x,y,dis,s)告訴你y在x的正s(s=E W S N 分別表示東西南北)方向的dis米。

而後q次查詢,每次查詢x,y,z,問你在第z條邊插入以前,是否知道點x和點y的曼哈頓距離,若是知道輸出,不然輸出-1。

思路:

顯然是現將詢問離線按z排序,再用帶權並查集記錄各節點與它所在的連通塊的節點的根節點的信息。

注意find和union函數的修改,必定要想清楚,這其中我經歷了若干發WA,還要注意初始化。

代碼:

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
using namespace std;
const int maxn=80510;
int n,t,m,k,q;
bool ok[maxn];
int fa[maxn],nxt[maxn];
int dx[maxn],dy[maxn],he[maxn],cnt;
int ans[maxn];
struct Edge
{
    int u,v,x,y,z;
    char s[2];
}edge[maxn];
int find(int x)
{
    if(fa[x]==-1) return x;
    int t=fa[x];
    fa[x]=find(t);
    dx[x]+=dx[t];
    dy[x]+=dy[t];
    return fa[x];
}
void un(int x)
{
    Edge e=edge[x];
    int fx=find(e.u);
    int fy=find(e.v);
    if(fx!=fy)
    {
        dx[fy]=dx[e.u]+e.x-dx[e.v];
        dy[fy]=dy[e.u]+e.y-dy[e.v];
        fa[fy]=fx;
    }
}
struct node
{
    int u,v;
    int tim,id;
    bool operator<(node aa)const
    {
        return tim<aa.tim;
    }
}query[maxn];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(fa,-1,sizeof(fa));
        memset(ok,0,sizeof(ok));
        memset(dx,0,sizeof(dx));
        memset(dy,0,sizeof(dy));
        char s[2];
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d%s",&edge[i].u,&edge[i].v,&edge[i].z,&edge[i].s);
            if(edge[i].s[0]=='E')
            {
                edge[i].x=edge[i].z;
                edge[i].y=0;
            }
            else if(edge[i].s[0]=='W')
            {
                edge[i].x=-edge[i].z;
                edge[i].y=0;
            }
            else if(edge[i].s[0]=='N')
            {
                edge[i].x=0;
                edge[i].y=edge[i].z;
            }
            else if(edge[i].s[0]=='S')
            {
                edge[i].x=0;
                edge[i].y=-edge[i].z;
            }
        }
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            query[i].id=i;
            scanf("%d%d%d",&query[i].u,&query[i].v,&query[i].tim);
        }
        sort(query,query+q);
        int r=0,l=1;
        while(r<q&&query[r].tim==0) {ans[query[r].id]=-1;r++;}
        for(int i=1;i<=m&&r<q;i++)
        {
            while(l<=query[r].tim)
            {
                un(l);
                l++;
            }
            while(r<q&&query[r].tim==i)
            {
                int fx=find(query[r].u);
                int fy=find(query[r].v);
                if(fx!=fy){ans[query[r].id]=-1;}
                else {ans[query[r].id]=abs(dx[query[r].u]-dx[query[r].v])+abs(dy[query[r].u]-dy[query[r].v]);}
                r++;
            }
        }
        for(int i=0;i<q;i++)
        {
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}

 

本文分享 CSDN - LSD20164388。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索