POJ 1984 Navigation Nightmare
并查集,给n个点和m条边,每条边有方向和长度,再给q个询问,第i个询问查询两个点之间在Ti时刻时的曼哈顿距离(能连通则输出曼哈顿距离,否则输出-1)
这题跟Corporative Network 有点像,只不过那题是维护到根节点的距离,这题还要顺便维护与根节点的x,y方向的偏移量。findset时,每次找完father就要加上father的x、y坐标偏移量,这样findset完以后就得到了与根的偏移量。然后合并时, (注意,这里是 fa[x] = y)
dr[x].x = r.x - dr[r.u].x + dr[r.v].x;
dr[x].y = r.y - dr[r.u].y + dr[r.v].y; 即 x->y <==> u->v - u->x + v->y 如下图:
这题还要注意数据可能不是按照时间顺序输入的,要做一个排序,然后再按原来的顺序输出。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 40100int fa[N],q;struct DR {int x,y; }dr[N],to[27];struct ROAD {int u,v;int x,y; }road[N];struct QUERY {int a,b,time;int id,res; }query[N];void makeset(int n) {for(int i=1;i<=n;i++){fa[i] = i;dr[i].x = dr[i].y = 0;} }int findset(int x) {if(x != fa[x]){int tmp = fa[x];fa[x] = findset(fa[x]);dr[x].x += dr[tmp].x;dr[x].y += dr[tmp].y;}return fa[x]; }void unionset(int index) //合并第index条边 {ROAD r = road[index];int x = findset(r.u);int y = findset(r.v);if(x == y)return;fa[x] = y;dr[x].x = r.x - dr[r.u].x + dr[r.v].x; // x->y <==> u->v - u->x + v->ydr[x].y = r.y - dr[r.u].y + dr[r.v].y; }int cmp1(QUERY ka,QUERY kb) {return ka.time<kb.time; }int cmp2(QUERY ka,QUERY kb) {return ka.id<kb.id; }void InitDirection() {to['E'-'A'].x = 1;to['E'-'A'].y = 0;to['W'-'A'].x = -1;to['W'-'A'].y = 0;to['N'-'A'].x = 0;to['N'-'A'].y = 1;to['S'-'A'].x = 0;to['S'-'A'].y = -1; }void read() {int n,m,i,dis;char ss[4];InitDirection();scanf("%d%d",&n,&m);makeset(n);for(i=1;i<=m;i++){scanf("%d%d%d %s",&road[i].u,&road[i].v,&dis,ss);road[i].x = dis*(to[ss[0]-'A'].x);road[i].y = dis*(to[ss[0]-'A'].y);}scanf("%d",&q);for(i=1;i<=q;i++){scanf("%d%d%d",&query[i].a,&query[i].b,&query[i].time);query[i].id = i;}sort(query+1,query+q+1,cmp1); }void solve() {int i,j;j=1;for(i=1;i<=q;i++){for(;j<=query[i].time;j++){unionset(j);}if(findset(query[i].a) != findset(query[i].b))query[i].res = -1;elsequery[i].res = abs(dr[query[i].a].x-dr[query[i].b].x) + abs(dr[query[i].a].y-dr[query[i].b].y);}sort(query+1,query+q+1,cmp2);for(i=1;i<=q;i++)cout<<query[i].res<<endl; }int main() {read();solve();return 0; }
View Code
转载于:https://www.cnblogs.com/whatbeg/p/3503585.html
POJ 1984 Navigation Nightmare相关推荐
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- POJ 1984 Navigation Nightmare 多权值并查集
一.内容 Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labe ...
- POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K T ...
- 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
[bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- bzoj 3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦(加权并查集)
3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 121 ...
- BZOJ 3362 Navigation Nightmare 带权并查集
题目大意:给定一些点之间的位置关系,求两个点之间的曼哈顿距离 此题土豪题.只是POJ也有一道相同的题,能够刷一下 别被题目坑到了,这题不强制在线.把询问离线处理就可以 然后就是带权并查集的问题了.. ...
- bzoj3362[Usaco2004 Feb]Navigation Nightmare 导航噩梦
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3362 题目大意: 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤400 ...
- kuangbin带你飞专题合集
题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...
- 算法学习经典例题整理
陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...
最新文章
- 添加三维动画 demo
- 用python+tornado+mongodb写的一个博客系统
- 针对Selenium环境搭建的一些小解说
- record not found or changed by another user 解决方法
- 基于mysql的主从复制之Mycat简单配置和高可用
- Jmeter 参数化请求实例
- Java基础-------集合框架
- Google的语音识别API,支持各种语言
- JVM运行机制理解整理
- 线束测试仪新应用:针对于高铁级电动“新巴客”的线束测试
- 词法分析器java语言_Java 实现词法分析器
- 使用tinyxml2将gps经纬度转换为kml和gpx格式文件
- Vista 如何关闭UAC(用户帐户控制)
- 5G物联网数据网关助力工业企业转型升级
- 这几个好用的简单流程图模板,你可不能错过
- 计算机英语读音在线,computer是什么意思_computer翻译_读音_用法_翻译
- CleanCode-函数
- python爬虫中字符串开头b,u,r的含义
- 第七章 绘制3D图表和统计地图
- 27年,IE时代终落幕
热门文章
- docker MySQL-错误:2059-Authentication plugin ‘caching_sha2_password‘ cannot be loaded
- linux查看磁盘使用情况及每个文件夹占用磁盘大小
- springcloud断点续传源码_SpringCloud 超大文件上传和断点续传的实现
- 【sklearn第八讲】广义线性模型
- 利用爬虫获取网上医院药品价格信息 (上)
- 20171018校内训练
- wordpress学习(四)---url伪静态简单了解
- Mimikatz 攻防杂谈
- 1006 换个格式输出整数 (15)
- ORACLE与.NET类型对应关系(转)