[USACO 2004DEC] Navigation Nightmare
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=3362
[算法]
带权并查集
时间复杂度 : O(NlogN)
[代码]
- #include<bits/stdc++.h>
- using namespace std;
- const int MAXN = ;
- struct Que
- {
- int f1 , f2 , t;
- int id;
- } que[MAXN];
- int n , m;
- int f[MAXN] , dist[MAXN] , x[MAXN] , y[MAXN] , X[MAXN] , Y[MAXN] , d[MAXN];
- int ans[MAXN];
- char dir[MAXN][];
- template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
- template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
- template <typename T> inline void read(T &x)
- {
- T f = ; x = ;
- char c = getchar();
- for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
- for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
- x *= f;
- }
- inline bool cmp(Que a,Que b)
- {
- return a.t < b.t;
- }
- inline int get_root(int x)
- {
- if (f[x] == x) return x;
- int fa = get_root(f[x]);
- X[x] += X[f[x]];
- Y[x] += Y[f[x]];
- return f[x] = fa;
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- for (int i = ; i <= n; i++) f[i] = i;
- for (int i = ; i <= m; i++) scanf("%d%d%d%s",&x[i],&y[i],&d[i],&dir[i]);
- int q;
- read(q);
- for (int i = ; i <= q; i++)
- {
- scanf("%d%d%d",&que[i].f1 , &que[i].f2 , &que[i].t);
- que[i].id = i;
- }
- sort(que + ,que + q + ,cmp);
- int cur = ;
- for (int i = ; i <= q; i++)
- {
- while (cur <= que[i].t)
- {
- int sx = get_root(x[cur]) , sy = get_root(y[cur]);
- if (sx == sy) continue;
- f[sx] = sy;
- if (dir[cur][] == 'E')
- {
- X[sx] = -X[x[cur]] + X[y[cur]] - d[cur];
- Y[sx] = -Y[x[cur]] + Y[y[cur]];
- }
- if ( dir[cur][] == 'W' )
- {
- X[sx] = -X[x[cur]] + X[y[cur]] + d[cur];
- Y[sx] = -Y[x[cur]] + Y[y[cur]];
- }
- if (dir[cur][] == 'N')
- {
- Y[sx] = -Y[x[cur]] + Y[y[cur]] - d[cur];
- X[sx] = -X[x[cur]] + X[y[cur]] ;
- }
- if (dir[cur][] == 'S')
- {
- Y[sx] = -Y[x[cur]] + Y[y[cur]] + d[cur];
- X[sx] = -X[x[cur]] + X[y[cur]];
- }
- ++cur;
- }
- if (get_root(que[i].f1) != get_root(que[i].f2)) ans[que[i].id] = -;
- else ans[que[i].id] = abs(X[que[i].f1] - X[que[i].f2]) + abs(Y[que[i].f1] - Y[que[i].f2]);
- }
- for (int i = ; i <= q; i++) printf("%d\n",ans[i]);
- return ;
- }
[USACO 2004DEC] Navigation Nightmare的更多相关文章
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- POJ 1984 Navigation Nightmare (数据结构-并检查集合)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4072 Accepted: 1 ...
- POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K T ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
[bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...
- [POJ1984]Navigation Nightmare
[POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
随机推荐
- [luoguP3258] [JLOI2014]松鼠的新家(lca + 树上差分)
传送门 需要把一条路径上除了终点外的所有数都 + 1, 比如,给路径 s - t 上的权值 + 1,可以先求 x = lca(s,t) 类似数列上差分的思路,可以给 s 和 f[t] 的权值 + 1, ...
- bzoj1059:[ZJOI2007]矩阵游戏【二分图匹配】
Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两 ...
- Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]
传送门 E. Anya and Cubes time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- jquery制作图片瀑布流点击按钮加载更多内容
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> ...
- 解决idea中启动tomcat出现控制台乱码问题
尝试了很多方法,最后终于解决了,现在提供给大家一个我认为最简单也最有效的方案. 1.修改配置文件 找到idea的安装目录,在bin文件夹下找到以下两个文件,用记事本或者其他软件打开: 然后两个文件中都 ...
- 2018 11.2 PION模拟赛
期望:100 + 50 + 30 = 180 实际:0 + 50 + 30 =80 期望:100 实际:0 数值有负数,边界应该设为-0x7f 此处 gg /* 期望的分:50+ */ ...
- rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnec
在mysql5中,可以设置safe mode,比如在一个更新语句中 UPDATE table_name SET bDeleted=0; 执行时会错误,报: You are using safe upd ...
- css实现文字渐变
css文件渐变虽然兼容性比较差,但是用在移动端和chrome中还是没有问题的. 实现文件渐变的方法有两种 1. 使用 background 的属性 2. 使用 mask 属性 方式一. <!DO ...
- Feign简介
Feign简介
- Ansible 详细用法说明(一)
一.概述 运维工具按需不需要有代理程序来划分的话分两类: agent(需要有代理工具):基于专用的agent程序完成管理功能,puppet, func, zabbix agentless(无须代理工具 ...