Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10501 | Accepted: 3429 |
Description
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
Output
Sample Input
0 0 10000 1000
0 200 5000 200 7000 200 -1 -1
2000 600 5000 600 10000 600 -1 -1
Sample Output
21
题目大意:首先给你两个坐标,一个是你家里的坐标,一个是你学校的坐标,然后接下来有若干条地铁线,每条地铁线上有若干个站点,给出每个站点
的坐标,有这些点,这些点当中有距离,这个距离的单位是米,现在告诉你走路是10km/h,做地铁的话是40km/h,问你从家里到学校所花费的最短时
间(分钟)。
解题思路:这个主要是卡的输入,只要将输入弄好了,构成一张图就好做了,然后需要注意的是,对于地铁线,你从1站到2站的速度是40km/h,
但是不代表你从1站到3站的速度是40km/h,你必须经过2站才能到3站,然后我们构图的时候权值保存的是时间多少分钟(可能会有从a到b点有多个
分钟,取最小的),这里单位换算也要注意下。
代码如下:
#include<stdio.h> #include<string.h> #include<math.h> #define N 230 #define INF 0x3f3f3f3f struct p { double x,y; } q[10010]; double e[N][N],dis[N]; int vis[N],n; void floyd() { for(int k=0; k<n; k++) for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(e[i][j]>e[i][k]+e[k][j]) e[i][j]=e[i][k]+e[k][j]; printf("%.0f\n",e[0][1]); } int main() { int i,j,m; scanf("%lf %lf %lf %lf",&q[0].x,&q[0].y,&q[1].x,&q[1].y);
memset(e,INF,sizeof(e));
for(i=0; i<=230; i++)
e[i][i]=0;
n=2;
m=0;
double s;
while(~scanf("%lf %lf",&q[n].x,&q[n].y))
{
if(q[n].x==-1&&q[n].y==-1)
{
m=0;
continue;
}
if(!m)//不在一条线上的站点时
{
m=1;
n++;//还需加1,不在一条线上的站点时只能仍步行来弥补,后面还得把他变为步行的(时间)权值
continue;
}
s=sqrt((q[n].x-q[n-1].x)*(q[n].x-q[n-1].x)+(q[n].y-q[n-1].y)*(q[n].y-q[n-1].y));//同一条线上相邻站点的距离
e[n][n-1]=e[n-1][n]=s*6/4000;//单位换算原型为s*60/40000m(分钟)
n++;
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)//站点间不能直接到达的,用步行来弥补
{
if(e[i][j]==INF)
{
s=sqrt((q[i].x-q[j].x)*(q[i].x-q[j].x)+(q[i].y-q[j].y)*(q[i].y-q[j].y));
e[i][j]=e[j][i]=s*6/1000;
}
}
floyd();
return 0;
}