A friend of yours, an inventor, has built a spaceship recently and wants to explore space with it. 
During his first voyages, he discovered that the universe is full of wormholes created by some alien race. These wormholes allow one to travel to places far, far away, but moreover, they can also send you to times long ago or in the distant future. 
Having mapped these wormholes and their respective end points, you and your friend boldly decide to board his spaceship and go to some distant place you'd like to visit. Of course, you want to arrive at your destination as early as possible. The question is: what is this earliest arrival time?

Input

The first line of input contains an integer c (1 <= c <= 200), the number of test cases. Each test case starts with a line containing two coordinate triples x0, y0, z0 and x1, y1, z1, the space coordinates of your departure point and destination. The next line contains an integer n (0 <= n <= 50), the number of wormholes. Then follow n lines, one for each wormhole, with two coordinate triples xs, ys, zs and xe, ye, ze, the space coordinates of the wormhole entry and exit points, respectively, followed by two integers t, d ( -1 000 000 <= t, d <= 1 000 000), the creation time t of the wormhole and the time shift d when traveling through the wormhole. 
All coordinates are integers with absolute values smaller than or equal to 10 000 and no two points are the same. 
Note that, initially, the time is zero, and that tunneling through a wormhole happens instantly. For simplicity, the distance between two points is deffned as their Euclidean distance (the square root of the sum of the squares of coordinate differences) rounded up to the nearest integer. Your friend's spaceship travels at speed 1.

Output

For each test case, print a single line containing an integer: the earliest time you can arrive at your destination.

Sample Input

  1. 2
  2. 0 0 0 100 0 0
  3. 2
  4. 1 1 0 1 2 0 -100 -2
  5. 0 1 0 100 1 0 -150 10
  6. 0 0 0 10 0 0
  7. 1
  8. 5 0 0 -5 0 0 0 0

Sample Output

  1. -89
  2. 10
  3.  
  4. 代码:
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<stack>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<cmath>
  11. #define Inf 0x3f3f3f3f
  12.  
  13. const int maxn=1e5+;
  14. typedef long long ll;
  15. using namespace std;
  16.  
  17. struct Edge
  18. {
  19. int u,v,w;
  20. int next;
  21. }edge[];
  22. int head[],vis[],cnt[],dis[],tot;
  23. void init()
  24. {
  25. memset(head,-,sizeof(head));
  26. memset(cnt,,sizeof(cnt));
  27. memset(vis,,sizeof(vis));
  28. memset(dis,Inf,sizeof(dis));
  29. }
  30. void add(int u,int v,int w)
  31. {
  32. edge[tot].u=u;
  33. edge[tot].v=v;
  34. edge[tot].w=w;
  35. edge[tot].next=head[u];
  36. head[u]=tot++;
  37. return;
  38. }
  39. int n,m,k;
  40. bool SPFA(int s)
  41. {
  42. dis[s]=;
  43. cnt[s]=;
  44. vis[s]=true;
  45. queue<int>q;
  46. q.push(s);
  47. while(!q.empty())
  48. {
  49. int now=q.front();
  50. q.pop();
  51. vis[now]=false;
  52. for(int i=head[now];i!=-;i=edge[i].next)
  53. {
  54. if(dis[now]+edge[i].w<dis[edge[i].v])
  55. {
  56. dis[edge[i].v]= dis[now]+edge[i].w;
  57. int y=edge[i].v;
  58. cnt[y]=cnt[now]+;
  59. if(cnt[y]>n)
  60. {
  61. return true;
  62. }
  63. if(vis[y]==false)
  64. {
  65. vis[y]=true;
  66. q.push(y);
  67. }
  68. }
  69. }
  70. }
  71. return false;
  72.  
  73. }
  74.  
  75. int main()
  76. {
  77. int T;
  78. cin>>T;
  79. while(T--)
  80. {
  81. tot=;
  82. init();
  83. scanf("%d%d%d",&n,&m,&k);
  84. int u,v,w;
  85. for(int t=;t<m;t++)
  86. {
  87. scanf("%d%d%d",&u,&v,&w);
  88. add(u,v,w);
  89. add(v,u,w);
  90. }
  91. for(int t=;t<k;t++)
  92. {
  93. scanf("%d%d%d",&u,&v,&w);
  94. add(u,v,-w);
  95. }
  96. if(SPFA())
  97. {
  98. puts("YES");
  99. }
  100. else
  101. {
  102. puts("NO");
  103. }
  104.  
  105. }
  106.  
  107. }
  1.  

POJ - 3851-Wormholes(SPFA判负环)的更多相关文章

  1. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  2. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  3. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  4. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  5. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  6. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  7. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  8. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  9. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  10. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

随机推荐

  1. Kaggle-pandas(5)

    Data-types-and-missing-values 教程 Dtypes DataFrame或Series中列的数据类型称为dtype.您可以使用dtype属性来获取特定列的类型. 例如,我们可 ...

  2. Linux入门-基本概念

    本文主要介绍linux基础概念介绍,对基本概念了解后,更好入门. 这里主要介绍一下几个概念 什么是linux GNU项目和自由软件基金会 linux发行版 什么是linux   也许大家都已经知道,L ...

  3. 迭代列表不要For循环,这是Python列表推导式最基本的概念

    如果你还在使用 For 循环迭代列表,那么你需要了解了解列表推导式,看看它的基本概念都是什么. 列表解析式(List comprehension)或者称为列表推导式,是 Python 中非常强大和优雅 ...

  4. shell脚本的常用执行方式

    1.sh+脚本的相对路径 [jinghang@hadoop101 datas]$ sh helloworld.sh helloworld sh+脚本的绝对路径 [jinghang@hadoop101 ...

  5. C#LeetCode刷题-回溯算法

    回溯算法篇 # 题名 刷题 通过率 难度 10 正则表达式匹配   18.8% 困难 17 电话号码的字母组合   43.8% 中等 22 括号生成   64.9% 中等 37 解数独   45.8% ...

  6. 初始JAVA第十五章String的总结

    字符串的概述 1.什么是字符串:零个或多个字符组成的有限序列 2.如何使用字符串:(使用字符串分为两步) 1)定义并初始化字符串 2)使用字符,对字符串进行一些处理 // 穿件一个字符串 // 语法 ...

  7. JavaScript基础-05-数组、Date对象、Math、包装类、字符串方法

    数组(Array) 1. 数组 数组也是一个对象,不同点:普通对象是使用字符串作为属性名的,数组是使用数字作为索引操作元素. 数组的存储性能比普通对象要好,在开发中经常使用数组来存储一些数据. 向数组 ...

  8. C#连接Oracle数据库,通过EF自动生成与数据库表相关的实体类

    C#连接Oracle数据库,通过EF自动生成与数据库表相关的实体类 ps:如需转载,请在转载文章明显处,i标注作者和原文地址 一.准备条件 需要自己电脑上已经安装了Oracle数据库,并且已经创建了相 ...

  9. [源码解析] 当 Java Stream 遇见 Flink

    [源码解析] 当 Java Stream 遇见 Flink 目录 [源码解析] 当 Java Stream 遇见 Flink 0x00 摘要 0x01 领域 1.1 Flink 1.2 Java St ...

  10. 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇)

    系列文章 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇) 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇) 扫码体验,先睹为快 可以扫描下微信小程序的 ...