You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
0 0 10000 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
21
题意:从家到学校,可以骑车,也可以做地铁,因为不能迟到,所以必须在最短的时间到达学校,第一行给出的数据是家和学校的坐标,下面每行是一列地铁的各个站点的标标,以-1,-1结束一列地铁站点。
思路:第一次接触这种题型,根本想不到这种方法,上网查了一下,只写了代码,可以看一下。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define INF 0x3f3f3f3f
double map[1000][1000],vis[1000];
int n,m,book[1100];
double min(double t1,double t2)
{
return t1<t2?t1:t2;
}
struct note
{
double x;
double y;
} Q[100000];
double F(double x1,double y1,double x2,double y2)
{
double term=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return sqrt(term);
}
void dijkstra()
{
for(int i=1; i<=n; i++)
{
vis[i]=map[1][i];
book[i]=0;
}
book[1]=1;
vis[1]=0;
for(int i=1; i<n; i++)
{
double mini=INF;
int t=-1;
for(int j=1; j<=n; j++)
{
if(!book[j]&&vis[j]<mini)
mini=vis[t=j];
}
if(mini==INF)
break;
book[t]=1;
for(int j=1; j<=n; j++)
{
if(!book[j]&&vis[j]>mini+map[t][j])
vis[j]=mini+map[t][j];
}
}
printf("%.0lf\n",vis[2]);
}
int main()
{
double v1=10000.0/60;
double v2=40000.0/60;
while(~scanf("%lf %lf %lf %lf",&Q[1].x,&Q[1].y,&Q[2].x,&Q[2].y))
{
int x,y,k=3;
for(int i=1; i<=400; i++)
for(int j=1; j<=400; j++)
{
map[i][j]=INF;
map[i][i]=0;
}
n=3;
while(~scanf("%d %d",&x,&y))
{
if(x==-1&&y==-1)
{
k=n;
continue;
}
Q[n].x=x;
Q[n].y=y;
if(k!=n)
map[n][n-1]=map[n-1][n]=min(map[n][n-1],F(Q[n].x,Q[n].y,Q[n-1].x,Q[n-1].y)/v2);
n++;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
map[i][j]=min(map[i][j],F(Q[i].x,Q[i].y,Q[j].x,Q[j].y)/v1);
}
dijkstra();
}
return 0;
}