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?
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.
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+5; typedef long long ll; using namespace std; struct Edge { int u,v,w; int next; }edge[7499]; int head[505],vis[505],cnt[505],dis[505],tot; void init() { memset(head,-1,sizeof(head)); memset(cnt,0,sizeof(cnt)); memset(vis,0,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]=0; cnt[s]=1; 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!=-1;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]+1; 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=0; init(); scanf("%d%d%d",&n,&m,&k); int u,v,w; for(int t=0;t<m;t++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } for(int t=0;t<k;t++) { scanf("%d%d%d",&u,&v,&w); add(u,v,-w); } if(SPFA(1)) { puts("YES"); } else { puts("NO"); } } }