[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=3362

[算法]

带权并查集

时间复杂度 : O(NlogN)

[代码]

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = ;
  4.  
  5. struct Que
  6. {
  7. int f1 , f2 , t;
  8. int id;
  9. } que[MAXN];
  10.  
  11. int n , m;
  12. int f[MAXN] , dist[MAXN] , x[MAXN] , y[MAXN] , X[MAXN] , Y[MAXN] , d[MAXN];
  13. int ans[MAXN];
  14. char dir[MAXN][];
  15.  
  16. template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
  17. template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
  18. template <typename T> inline void read(T &x)
  19. {
  20. T f = ; x = ;
  21. char c = getchar();
  22. for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  23. for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
  24. x *= f;
  25. }
  26. inline bool cmp(Que a,Que b)
  27. {
  28. return a.t < b.t;
  29. }
  30. inline int get_root(int x)
  31. {
  32. if (f[x] == x) return x;
  33. int fa = get_root(f[x]);
  34. X[x] += X[f[x]];
  35. Y[x] += Y[f[x]];
  36. return f[x] = fa;
  37. }
  38.  
  39. int main()
  40. {
  41.  
  42. scanf("%d%d",&n,&m);
  43. for (int i = ; i <= n; i++) f[i] = i;
  44. for (int i = ; i <= m; i++) scanf("%d%d%d%s",&x[i],&y[i],&d[i],&dir[i]);
  45. int q;
  46. read(q);
  47. for (int i = ; i <= q; i++)
  48. {
  49. scanf("%d%d%d",&que[i].f1 , &que[i].f2 , &que[i].t);
  50. que[i].id = i;
  51. }
  52. sort(que + ,que + q + ,cmp);
  53. int cur = ;
  54. for (int i = ; i <= q; i++)
  55. {
  56. while (cur <= que[i].t)
  57. {
  58. int sx = get_root(x[cur]) , sy = get_root(y[cur]);
  59. if (sx == sy) continue;
  60. f[sx] = sy;
  61. if (dir[cur][] == 'E')
  62. {
  63. X[sx] = -X[x[cur]] + X[y[cur]] - d[cur];
  64. Y[sx] = -Y[x[cur]] + Y[y[cur]];
  65. }
  66. if ( dir[cur][] == 'W' )
  67. {
  68. X[sx] = -X[x[cur]] + X[y[cur]] + d[cur];
  69. Y[sx] = -Y[x[cur]] + Y[y[cur]];
  70. }
  71. if (dir[cur][] == 'N')
  72. {
  73. Y[sx] = -Y[x[cur]] + Y[y[cur]] - d[cur];
  74. X[sx] = -X[x[cur]] + X[y[cur]] ;
  75. }
  76. if (dir[cur][] == 'S')
  77. {
  78. Y[sx] = -Y[x[cur]] + Y[y[cur]] + d[cur];
  79. X[sx] = -X[x[cur]] + X[y[cur]];
  80. }
  81. ++cur;
  82. }
  83. if (get_root(que[i].f1) != get_root(que[i].f2)) ans[que[i].id] = -;
  84. else ans[que[i].id] = abs(X[que[i].f1] - X[que[i].f2]) + abs(Y[que[i].f1] - Y[que[i].f2]);
  85. }
  86. for (int i = ; i <= q; i++) printf("%d\n",ans[i]);
  87.  
  88. return ;
  89.  
  90. }

[USACO 2004DEC] Navigation Nightmare的更多相关文章

  1. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  2. POJ 1984 Navigation Nightmare (数据结构-并检查集合)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1 ...

  3. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  4. POJ 1984 Navigation Nightmare 带全并查集

    Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

  5. BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集

    BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description     农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...

  6. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  7. POJ1984:Navigation Nightmare(带权并查集)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7871   Accepted: 2 ...

  8. 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦

    [bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 ​ 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...

  9. [POJ1984]Navigation Nightmare

    [POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

随机推荐

  1. [luoguP3258] [JLOI2014]松鼠的新家(lca + 树上差分)

    传送门 需要把一条路径上除了终点外的所有数都 + 1, 比如,给路径 s - t 上的权值 + 1,可以先求 x = lca(s,t) 类似数列上差分的思路,可以给 s 和 f[t] 的权值 + 1, ...

  2. bzoj1059:[ZJOI2007]矩阵游戏【二分图匹配】

    Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两 ...

  3. Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]

    传送门 E. Anya and Cubes time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. jquery制作图片瀑布流点击按钮加载更多内容

    <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script> ...

  5. 解决idea中启动tomcat出现控制台乱码问题

    尝试了很多方法,最后终于解决了,现在提供给大家一个我认为最简单也最有效的方案. 1.修改配置文件 找到idea的安装目录,在bin文件夹下找到以下两个文件,用记事本或者其他软件打开: 然后两个文件中都 ...

  6. 2018 11.2 PION模拟赛

    期望:100 + 50 + 30 = 180 实际:0 + 50 + 30 =80 期望:100   实际:0 数值有负数,边界应该设为-0x7f       此处 gg /* 期望的分:50+ */ ...

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

  8. css实现文字渐变

    css文件渐变虽然兼容性比较差,但是用在移动端和chrome中还是没有问题的. 实现文件渐变的方法有两种 1. 使用 background 的属性 2. 使用 mask 属性 方式一. <!DO ...

  9. Feign简介

    Feign简介

  10. Ansible 详细用法说明(一)

    一.概述 运维工具按需不需要有代理程序来划分的话分两类: agent(需要有代理工具):基于专用的agent程序完成管理功能,puppet, func, zabbix agentless(无须代理工具 ...