POJ-1984 Navigation Nightmare
来源:互联网 发布:timbuk2什么档次知乎 编辑:程序博客网 时间:2024/05/16 00:36
题目链接:Navigation Nightmare
题目大意:按照时间顺序给一些点的相对位置。有若干询问,每个询问给出两个点的编号以及时间,若在此时间内该两点相对位置确定则输出其曼哈顿距离,否则输出-1。
解题思路:很好的一道带权并查集问题,由于询问中的时间条件我纠结了很久,最后参考网上代码,按照询问的时间顺序来合并点并保存答案就行了。注意在更新相对位置的时候最好自己画一下图。
代码如下:
#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef pair<int, int> P;const int inf = 0x3f3f3f3f;const int maxm = 1e4 + 15;const int maxn = 4e4 + 15;struct Node{ int x, y, id, t; bool operator < (const Node& rhs) const { return t < rhs.t; }}node[maxm];int dx[maxn], dy[maxn], rx[maxn], ry[maxn], x[maxn], y[maxn];int par[maxn], ans[maxm];int find(int x) { if(par[x] == x) return x; int res = find(par[x]); int fa = par[x]; // root -> fa + fa -> x ==> root -> x rx[x] = rx[fa] + rx[x]; ry[x] = ry[fa] + ry[x]; return par[x] = res;}inline int read() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x;}int main(){#ifdef NEKO freopen("Nya.txt", "r", stdin);#endif int n, m, k; cin >> n >> m; for(int i = 1; i <= n; i++) { par[i] = i; // relative position rx[i] = ry[i] = 0; } for(int i = 1; i <= m; i++) { int d; char c; x[i] = read(); y[i] = read(); d = read(); scanf("%c", &c); switch(c) { case 'W': dx[i] = -d; dy[i] = 0; break; case 'S': dx[i] = 0; dy[i] = -d; break; case 'E': dx[i] = d; dy[i] = 0; break; case 'N': dx[i] = 0; dy[i] = d; break; } } k = read(); for(int i = 1; i <= k; i++) { node[i].x = read(); node[i].y = read(); node[i].t = read(); node[i].id = i; } sort(node + 1, node + 1 + k); int j = 1; for(int i = 1; i <= k; i++) { for(; j <= node[i].t; j++) { int l = find(x[j]), r = find(y[j]); if(l != r) { par[l] = r; // root -> l <== root -> y + y -> x + x -> l rx[l] = rx[y[j]] + dx[j] - rx[x[j]]; ry[l] = ry[y[j]] + dy[j] - ry[x[j]]; } } if(find(node[i].x) != find(node[i].y)) ans[node[i].id] = -1; else ans[node[i].id] = abs(rx[node[i].x] - rx[node[i].y]) + abs(ry[node[i].x] - ry[node[i].y]); } for(int i = 1; i <= k; i++) printf("%d\n", ans[i]); return 0;}
0 0
- POJ 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ-1984-Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ-1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- Navigation Nightmare poj 1984
- poj 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare 笔记
- 【POJ 1984】Navigation Nightmare (并查集)
- POJ--1984-Navigation Nightmare--并查集
- poj 1984 - Navigation Nightmare(并查集)
- POJ 1984 Navigation Nightmare 并查集
- Navigation Nightmare POJ
- [POJ P1984]Navigation Nightmare
- static 和extrn 对函数的作用
- XHTML表单(二)
- 【Linux】Linux添加系统调用以及内核编译过程
- 设计模式之简单的工厂模式
- python 错误和异常解析
- POJ-1984 Navigation Nightmare
- POJ1151Atlantis【离散化+扫描线+线段树】
- 堆排序的Java实现
- 35选7
- 学习算法的一个博客
- extern 和 static 对变量的作用
- DRBD概念详解及配置
- 对UIView的总结
- getline()函数详解