比较赤裸的最短路,最开始先构图,只有在同一地铁线上并相邻的点才有40km/h的时速,其它两点间距离都是步行速度10km/h,构图完之后就直接Dijkstra求最小时间了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
struct Stop
{
int x, y;
};
static double distance(struct Stop *s1, struct Stop *s2)
{
return sqrt(
(s1->x - s2->x) * (s1->x - s2->x)
+ (s1->y - s2->y) * (s1->y - s2->y));
}
int main()
{
struct Stop *array = malloc(210 * sizeof(struct Stop));
int map[210];
double time[210][210], d[210];
int known[210];
while (scanf("%d %d", &(array[0].x), &(array[0].y)) != EOF)
{
scanf("%d %d", &(array[1].x), &(array[1].y));
map[0] = map[1] = 0;
int total = 2, group = 0, xx, yy;
while (scanf("%d %d", &xx, &yy), xx != -1 && yy != -1)
{
group++;
array[total].x = xx;
array[total].y = yy;
map[total++] = group;
while (scanf("%d %d", &xx, &yy), xx != -1 && yy != -1)
{
array[total].x = xx;
array[total].y = yy;
map[total++] = group;
}
}
int i, j;
for (i = 0; i < total; i++)
for (j = i; j < total; j++)
{
if (map[i] == map[j] && map[i] && i + 1 == j)
time[i][j] = time[j][i] = distance(&array[i], &array[j])
* 0.0015;
else
time[i][j] = time[j][i] = distance(&array[i], &array[j])
* 0.006;
}
for (i = 0; i < total; i++)
d[i] = 1E20;
memset(known, 0, sizeof(known));
d[0] = 0;
int now = 0, next;
while (1)
{
known[now] = 1;
double mind = 1E20;
for (i = 0; i < total; i++)
{
if (!known[i] && d[now] + time[now][i] < d[i])
d[i] = d[now] + time[now][i];
if (!known[i] && d[i] < mind)
{
mind = d[i];
next = i;
}
}
now = next;
if (now == 1)
{
printf("%.0lf\n", d[now]);
break;
}
}
}
free(array);
return 0;
}