POJ1984 Navigation Nightmare ——并查集 注意细节

注意合并x y集合时的操作:找到根结点fx fy, fa[fx] = fy, 接下来修改fx点的权值,这个值要考虑到点x和y和x y间的关系,具体见代码74-77

先存下所有询问,然后离线做

POJ1984

题意:

有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1。

思路:

用east数组表示i到父节点需要向东走的距离,nor表示i到父节点需要向北走的距离(负值表示正西和正南),然后直接用带权并查集,询问时曼哈顿距离就是两个权值的绝对值之和。由于询问是嵌在给出关系中间的,所以要先存下所有关系和询问,离线做就行。

// Decline is inevitable,
// Romance will last forever.
// POJ1417
//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <vector>
using namespace std;
#define mst(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
//#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
//#define int long long
const int maxn = 4e4 + 10;
const int maxm = 25;
const int P = 1e9 + 7;
int fa[maxn];
struct node {
    int num, a, b, t;
    bool operator < ( const node &x) const {
        return t < x.t;
    }
}q[maxn];
int n, m;
int east[maxn]; //走east[i]到达根结点
int nor[maxn];  //走nor[i]到达根结点
int find(int x) {
    if(x == fa[x]) return x;
    int tmp = fa[x];
    fa[x] = find(fa[x]);
    east[x] = east[x] + east[tmp];
    nor[x] = nor[x] + nor[tmp];
    return fa[x];
}
int ans[10010];
int a[maxn], b[maxn], l[maxn];
char c[maxn][3];
int abs(int x) {
    return x > 0 ? x : -x;
}

void solve() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        fa[i] = i;
    for(int i = 1; i <= m; i++)
        scanf("%d%d%d%s", &a[i],&b[i],&l[i],c[i]);
    int k;
    scanf("%d", &k);
    for(int i = 1; i <= k; i++){
        scanf("%d%d%d", &q[i].a,&q[i].b, &q[i].t);
        q[i].num = i;
    }
    sort(q + 1, q + k + 1);
    int pos = 1;
    for(int i = 1; i <= m; i++) {
        if(pos == k + 1) break;
        int fx = find(a[i]);
        int fy = find(b[i]);
        if(fx != fy) {
            fa[fx] = fy;
            if(c[i][0] == 'E') east[fx] = l[i] + east[b[i]] - east[a[i]],nor[fx]=nor[b[i]]-nor[a[i]];
            else if(c[i][0] == 'W') east[fx] = -l[i] + east[b[i]] - east[a[i]],nor[fx]=nor[b[i]]-nor[a[i]];
            else if(c[i][0] == 'N') nor[fx] =l[i]+nor[b[i]]-nor[a[i]],east[fx]=east[b[i]]-east[a[i]];
            else nor[fx] = -l[i] + nor[b[i]] - nor[a[i]],east[fx]=east[b[i]]-east[a[i]];
        }
        while(i == q[pos].t) {
            int fx = find(q[pos].a);
            int fy = find(q[pos].b);
            if(fx != fy) {
                ans[q[pos].num] = -1;
                pos++;
                continue;
            }
            int dx = abs(east[q[pos].a] - east[q[pos].b]);
            int dy = abs(nor[q[pos].a] - nor[q[pos].b]);
            ans[q[pos].num] = dx + dy;
            pos++;
        }
    }
    for(int i = 1; i <= k; i++)
        printf("%d\n", ans[i]);
}
signed main() {
//    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//    int T; scanf("%d", &T); while(T--)
//    int T; cin >> T; while(T--)
    solve();
    return 0;
}

上一篇:Flash


下一篇:spi-mem