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

2
0 0 0 100 0 0
2
1 1 0 1 2 0 -100 -2
0 1 0 100 1 0 -150 10
0 0 0 10 0 0
1
5 0 0 -5 0 0 0 0

Sample Output

-89
10 代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f const int maxn=1e5+;
typedef long long ll;
using namespace std; struct Edge
{
int u,v,w;
int next;
}edge[];
int head[],vis[],cnt[],dis[],tot;
void init()
{
memset(head,-,sizeof(head));
memset(cnt,,sizeof(cnt));
memset(vis,,sizeof(vis));
memset(dis,Inf,sizeof(dis));
}
void add(int u,int v,int w)
{
edge[tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
return;
}
int n,m,k;
bool SPFA(int s)
{
dis[s]=;
cnt[s]=;
vis[s]=true;
queue<int>q;
q.push(s);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=false;
for(int i=head[now];i!=-;i=edge[i].next)
{
if(dis[now]+edge[i].w<dis[edge[i].v])
{
dis[edge[i].v]= dis[now]+edge[i].w;
int y=edge[i].v;
cnt[y]=cnt[now]+;
if(cnt[y]>n)
{
return true;
}
if(vis[y]==false)
{
vis[y]=true;
q.push(y);
}
}
}
}
return false; } int main()
{
int T;
cin>>T;
while(T--)
{
tot=;
init();
scanf("%d%d%d",&n,&m,&k);
int u,v,w;
for(int t=;t<m;t++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int t=;t<k;t++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,-w);
}
if(SPFA())
{
puts("YES");
}
else
{
puts("NO");
} } }
												

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. Linxu系统安装PHP详细教程

    安装centos源 yum install epel-release –y 下载php安装压缩包 wget https://www.php.net/distributions/php-7.3.15.t ...

  2. Docker-Compose介绍,安装和使用

    Docker-Compose 介绍 有时候运行一个镜像需要大量的参数,可以通过Docker-Compose编写这些参数.而且Docker-Compose可以版主我们批量管理容器,这些信息值需要通过一个 ...

  3. nvcc fatal : Path to libdevice library not specified

    安装完成后,配置环境变量,在home下的.bashrc中加入 export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH export ...

  4. Java 字节流

    OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法. OutputStream有很多子类,其中子类FileOutputStream ...

  5. C#LeetCode刷题之#63-不同路径 II​​​​​​​(Unique Paths II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...

  6. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

  7. 总结关于Ubuntu 安装 Docker 配置相关问题及解决方法

    总结关于Ubuntu 安装 Docker 配置相关问题及解决方法 Tomcat 示例 软件镜像(xx安装程序)----运行镜像----产生一个容器(正在运行的软件,运行的xx): 步骤: 1.搜索镜像 ...

  8. 顶级技术盛会KubeCon 2020,网易轻舟布道多云环境云原生应用交付

    在日前的KubeCon 2020中国线上峰会上,VMware中国研发中心架构师.Harbor项目创始人和维护者张海宁,和网易数帆轻舟事业部架构师.Harbor维护者裴明明,共同分享了如何在多云和多集群 ...

  9. Scala集合中的函数(扩展)

    1.拉链(zip) 将两个集合进行 对偶元组合并,可以使用拉链 object Demo_031 { def main(args: Array[String]): Unit = { val list1 ...

  10. JavaScript学习系列博客_28_JavaScript 数组去重

    数组去重 var arr = [1,2,3,2,2,1,3,4,2,5]; //去除数组中重复的数字 //获取数组中的每一个元素 for(var i=0 ; i<arr.length ; i++ ...