ZOJ-1891

比较赤裸的最短路,最开始先构图,只有在同一地铁线上并相邻的点才有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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值