题目大意:给定一些点之间的位置关系,求两个点之间的曼哈顿距离

此题土豪题。只是POJ也有一道相同的题,能够刷一下

别被题目坑到了,这题不强制在线。把询问离线处理就可以

然后就是带权并查集的问题了。。

将权值设为方向向量,重载+和-,依照正常权值并查集做即可了

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #define M 40400
  6. using namespace std;
  7. struct abcd{
  8. int x,y;
  9. abcd(){}
  10. abcd(int X,int Y):x(X),y(Y){}
  11. abcd operator + (const abcd &Y) const
  12. {
  13. return abcd( x+Y.x , y+Y.y );
  14. }
  15. abcd operator - (const abcd &Y) const
  16. {
  17. return abcd( x-Y.x , y-Y.y );
  18. }
  19. }f[M];
  20. struct operation{
  21. int x,y;
  22. abcd temp;
  23. }operations[M];
  24. struct query{
  25. int x,y,z,pos;
  26. bool operator < (const query &Y) const
  27. {
  28. return z < Y.z ;
  29. }
  30. }queries[10100];
  31. int n,m,q,fa[M],ans[10100];
  32. int Distance(abcd x)
  33. {
  34. return abs(x.x)+abs(x.y);
  35. }
  36. int Find(int x)
  37. {
  38. if(!fa[x]||fa[x]==x)
  39. return fa[x]=x;
  40. int y=fa[x];
  41. fa[x]=Find(fa[x]);
  42. f[x]=f[y]+f[x];
  43. return fa[x];
  44. }
  45. int main()
  46. {
  47. int i,j,x,y,z;
  48. char p[10];
  49. cin>>n>>m;
  50. for(i=1;i<=m;i++)
  51. {
  52. scanf("%d%d%d%s",&operations[i].x,&operations[i].y,&z,p);
  53. switch(p[0])
  54. {
  55. case 'E':operations[i].temp=abcd(z,0);break;
  56. case 'W':operations[i].temp=abcd(-z,0);break;
  57. case 'N':operations[i].temp=abcd(0,z);break;
  58. case 'S':operations[i].temp=abcd(0,-z);break;
  59. }
  60. }
  61. cin>>q;
  62. for(i=1;i<=q;i++)
  63. scanf("%d%d%d",&queries[i].x,&queries[i].y,&queries[i].z),queries[i].pos=i;
  64. sort(queries+1,queries+q+1);
  65. for(i=1,j=1;i<=q;i++)
  66. {
  67. for(;j<=queries[i].z;j++)
  68. {
  69. int x=operations[j].x;
  70. int y=operations[j].y;
  71. int fx=Find(x),fy=Find(y);
  72. fa[fy]=fx;
  73. f[fy]=f[x]-f[y]+operations[j].temp;
  74. }
  75. int x=queries[i].x;
  76. int y=queries[i].y;
  77. if( Find(x)!=Find(y) )
  78. ans[queries[i].pos]=-1;
  79. else
  80. ans[queries[i].pos]=Distance(f[x]-f[y]);
  81. }
  82. for(i=1;i<=q;i++)
  83. printf("%d\n",ans[i]);
  84. }

BZOJ 3362 Navigation Nightmare 带权并查集的更多相关文章

  1. POJ 1984 - Navigation Nightmare - [带权并查集]

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

  2. POJ-1984-Navigation Nightmare+带权并查集(中级

    传送门:Navigation Nightmare 参考:1:https://www.cnblogs.com/huangfeihome/archive/2012/09/07/2675123.html 参 ...

  3. BZOJ 2303 方格染色(带权并查集)

    要使得每个2*2的矩形有奇数个红色,如果我们把红色记为1,蓝色记为0,那么我们得到了这2*2的矩形里的数字异或和为1. 对于每个方格则有a(i,j)^a(i-1,j)^a(i,j-1)^a(i-1,j ...

  4. POJ 1984 Navigation Nightmare 带全并查集

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

  5. 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)

    题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...

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

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

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

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

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

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

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

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

随机推荐

  1. java基础讲解07-----数组

    1.什么是数组 2.怎么使用数组 package test; public class ShuZu {            public static void main(String[] args ...

  2. HIVE的transform函数的使用

    Hive的TRANSFORM关键字提供了在SQL中调用自写脚本的功能,适合实现Hive中没有的功能又不想写UDF的情况.例如,按日期统计每天出现的uid数,通常用如下的SQL SELECT date, ...

  3. python 2,3版本自动识别导入

     import sys if str(sys.version[0]) == "3":    from urllib.parse import quote_plus    from  ...

  4. SSM整合开发流程

    我的spring是3.2,mybatis是3.4 1 引入user libarary,我的jar文件如下 //spring mvc core springMVC\spring-web-.RELEASE ...

  5. IOS 拍照旋转修正

    - (UIImage *)fixOrientation:(UIImage *)aImage { // No-op if the orientation is already correct if (a ...

  6. applicationCache

    <html manifest="/m.appcache"> window.applicationCache.onupdateready = function (e) { ...

  7. html页面中js判断浏览器是否是IE浏览器及IE浏览器版本

    HTML里: HTML代码中,在编写网页代码时,各种浏览器的兼容性是个必须考虑的问题,有些时候无法找到适合所有浏览器的写法,就只能写根据浏览器种类区别的代码,这时就要用到判断代码了.在HTML代码中, ...

  8. Objective-C之@protocol

    转自:http://www.cnblogs.com/hxxy2003/archive/2011/10/24/2222838.html @protocol是Objective-C中的接口定义方式,也就是 ...

  9. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

  10. RequireJS 和 Sea.js

    相同之处 RequireJS 和 Sea.js 都是模块加载器,倡导模块化开发理念,核心价值是让 JavaScript 的模块化开发变得简单自然. 不同之处 两者的主要区别如下: 定位有差异.Requ ...