(advanced) 最短路 UVA 12227 Wormholes
来源:互联网 发布:鬼影 电影 知乎 编辑:程序博客网 时间:2024/05/16 07:20
Problem J
Wormholes
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 (-1000000 ≤ t,d ≤ 1000000), 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 10000 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 defined 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.
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
10
#include<iostream>#include<cstring>#include<cstdio>#include<string.h>#include<math.h>#include<vector>#include<queue>using namespace std;#define eps 1e-8#define LL long longconst int maxn = 1000;const int inf = 1e8;int n , m , size;bool vis[maxn] , inq[maxn];int d[maxn];int d_tmp[maxn];int c;struct Edge{Edge(int uu=0,int vv=0, int tt=0,int ww=0) : u(uu) , v(vv) , t(tt) ,w(ww) { }int u , v , w;int t;}S[maxn];struct Point{Point(int xx=0,int yy=0,int zz=0) : x(xx) , y(yy) , z(zz) { }int x , y , z;}point[maxn];inline double sqr(LL x) { return x * x; }int dist(const Point & p1,const Point & p2){return ceil(sqrt(sqr(p1.z-p2.z)+sqr(p1.x-p2.x)+sqr(p1.y-p2.y))-eps);}struct Node {int v;int t;int w;Node *next;} edge[maxn*maxn] , *first[maxn];void init() { m = 0 ; memset(first,0,sizeof(first)); }void add(int x,int y, int w,int t){edge[++m].v = y;edge[m].w = w;edge[m].t = t;edge[m].next = first[x];first[x] = &edge[m];}void input(){size = 0;int x , y , z;scanf("%d%d%d",&x,&y,&z);point[++size] = Point(x,y,z);scanf("%d%d%d",&x,&y,&z);point[++size] = Point(x,y,z);scanf("%d",&n);for (int i = 0 ; i < n ; ++i){scanf("%d%d%d",&x,&y,&z);point[++size] = Point(x,y,z);int u = size;scanf("%d%d%d",&x,&y,&z);point[++size] = Point(x,y,z);int v = size;int d , t;scanf("%d%d",&t,&d);add(u,v,d,t);}for (int i = 1 ; i <= size ; ++i) {for (int j = i+1 ; j <= size ; ++j) {add(i,j,dist(point[i],point[j]),-inf);add(j,i,dist(point[i],point[j]),-inf);}}}void dfs(int u,queue<int> & q){if (vis[u]) {int ret = c-1;int sum = 0;for (int i = c-1 ; ; --i){if (S[ret].t < S[i].t) ret = i;sum += S[i].w;if (S[i].u==u) break;}if (sum >= 0 || S[ret].t==-inf) return;d_tmp[S[ret].u] = S[ret].t;if (!inq[S[ret].u]) q.push(S[ret].u);inq[S[ret].u] = true;return; }vis[u] = true;for (Node * p = first[u] ; p ; p=p->next){int v = p->v , w = p->w;if (p->t > d[u]) w += p->t-d[u];if (d[v] <= d[u]+w) continue;d[v] = d[u]+w;S[c++] = Edge(u,v,p->t,w);dfs(v,q);--c;}vis[u] = false;return;}void solve(){memset(vis,0,sizeof(vis));memset(inq,0,sizeof(inq));queue<int> q;for (int i = 1 ; i <= size ; ++i) d[i] = inf;d[1] = 0;c = 0;dfs(1,q);if (!inq[1]) {inq[1] = true;q.push(1);}for (int i = 1 ; i <= size ; ++i) {if (!inq[i]) d[i] = inf;else d[i] = d_tmp[i];}while (q.size()){int u = q.front(); q.pop();inq[u] = false;for (Node * p = first[u] ; p ; p=p->next){int v = p->v , w = p->w;if (p->t > d[u]) w += p->t-d[u];if (d[v] <= d[u]+w) continue;d[v] = d[u]+w;if (inq[v]) continue;inq[v] = true; q.push(v);}}printf("%d\n",d[2]);//cout << d[2] << endl;}int main(){int T; cin>>T;while (T--){init();input();solve();}}
- (advanced) 最短路 UVA 12227 Wormholes
- (beginer) 最短路 UVA 558 Wormholes
- (advanced) UVA 最短路 10381 - The Rock
- POJ 3259 Wormholes(最短路Bellman_Ford)
- POJ3259 Wormholes 最短路(带负圈)
- poj Wormholes最短路问题(bellman_ford)
- POJ 3259 Wormholes(最短路)
- POJ 3259Wormholes(最短路bellman)
- Wormholes(最短路_bellman_ford)
- POJ3259----Wormholes(最短路)
- Poj_3259 Wormholes(最短路)
- poj3259 Wormholes(最短路)
- Wormholes(最短路)
- poj 3259 Wormholes 最短路
- poj3259 Wormholes 最短路bellman
- poj 3259 Wormholes 最短路
- POJ3259 Wormholes 最短路判负环
- POJ Wormholes 3259(最短路)
- (beginer) 最短路 UVA 658 It\'s not a Bug, it\'s a Feature!
- (intermediate)最短路(最长路) UVA 1348 - Tomato Automata
- (intermediate) 最短路(差分约束+拓扑排序) UVA 1516 - Smoking gun
- (intermediate)次短路 UVA 10342 - Always Late
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- (advanced) 最短路 UVA 12227 Wormholes
- (beginer) 最小生成树 UVA 1395 Slim Span
- CSDN感谢你!
- (beginer) 最小生成树 UVA 10600 ACM Contest and Blackout
- (beginer) 最小生成树 UVA 11747 Heavy Cycle Edges
- (beginer) 最小生成树 UVA 544 Heavy Cargo
- (intermediate) 最小生成树 UVA 10369 - Arctic Network
- (intermediate)最小生成树 UVA 12275 - Sensor network
- (intermediate) 最小生成树 UVA 1504 Genghis Khan the Conqueror