Test instructions: In a city, there are several subway lines, each subway line has a number of stations, all the speed of the subway is 40km/h. Now that you know the coordinates of the origin and the end points, and the coordinates of each site of these subway lines, your walking speed is 10km/h, and you get to the subway at any station. Ask you how much time you need at least from the starting point to the end.
There are many sites, the data given is just a line, so you need to preprocess the data first, Increase the time required for any two-point person to walk. Data pre-processing is more troublesome ...
#include <stdio.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespaceStd
ConstintMAXN =100005;
ConstintOO =0xFFFFFFF;
ConstDoublevs = +* +/60.0;//how many meters per minute.
ConstDoubleVP =Ten* +/60.0;
structNode
{
intU, V, next;
DoubleC//time required between two points
}E[MAXN];
structPoint
{
intx, y;
}p[1005];
intNpoint, head[1005];
Doubledis[1005];
BOOLvis[1005];
DoubleLen (Point A, point B,DoubleV
{
returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y))/V;
}
voidADD (intUintVDoubleLenintK
{
e[k].u = u;
E[K].V = v;
E[K].C = Len;
E[k].next = Head[u];
Head[u] = k;
}
voidSPFA ()
{
stack<int> STA;
Sta.push (1);
while(Sta.size ())
{
inti = Sta.top (); Sta.pop ();
Vis[i] =false;
for(intJ=head[i]; j!=0; J=e[j].next)
{
intU = e[j].u, v = e[j].v;
Doublec = e[j].c;
if(Dis[u]+c < dis[v])
{
DIS[V] = Dis[u] + C;
if(Vis[v] = =false)
{
VIS[V] =true;
Sta.push (v);
}
}
}
}
}
intMain ()
{
intI, J, flag=0, k=1;
scanf"%d%d%d%d", &p[1].x, &p[1].Y, &p[2].x, &p[2].Y);
Npoint =3;
while(SCANF ("%d%d", &p[npoint].x, &p[npoint].y)! = EOF)
{
if(p[npoint].x! =-1)
{
if(Flag = =0)
Flag =1;
Else
{
Doublec = Len (P[npoint], p[npoint-1], VS);
ADD (Npoint, npoint-1, C, k++);
ADD (npoint-1, Npoint, C, k++);
}
npoint++;
}
Else
Flag =0;
}
for(i=1; i<npoint; i++)
for(j=i+1; j<=npoint; J + +)
{
Doublec = Len (P[i], p[j], VP);
Add (I, J, C, k++);
Add (J, I, C, k++);
}
for(i=2; i<npoint; i++)
Dis[i] = oo;
SPFA ();
printf"%.0f\n", dis[2]);
return0;
}
L-subway-poj 2502