uva 10075 - Airlines(几何+最短路)

942 篇文章 2 订阅
99 篇文章 0 订阅

题目链接:uva 10075 - Airlines


在球面上处理出两点的距离,Floyd处理。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <algorithm>
//#include <iostream>

using namespace std;
/* d = r*sqrt(2-2*(cos(lat1)*cos(lat2)*cos(lon1-lon2)+sin(lat1)*sin(lat2)) */

typedef long long ll;
const int maxn = 105;
const double pi = 4 * atan(1);
const double eps = 1e-9;
const double R = 6378;
const int inf = 0x3f3f3f3f;

int N, M, Q, D[maxn][maxn];
char s[maxn], t[maxn];
map<string, int> G;
double lati[maxn], loti[maxn];

double getDistance(double a1, double b1, double a2, double b2) {
	a1 = a1 * pi / 180; a2 = a2 * pi / 180;
	b1 = b1 * pi / 180; b2 = b2 * pi / 180;
	double d = fabs(b1 - b2);
	if (d > pi)
		d = 2 * pi - d;
	return R*acos(cos(a1)*cos(a2)*cos(d)+sin(a1)*sin(a2));
}

void init () {
	G.clear();
	memset(D, inf, sizeof(D));
	for (int i = 1; i <= N; i++) {
		scanf("%s%lf%lf", s, &lati[i], &loti[i]);
		G[s] = i;
		D[i][i] = 0;
	}

	for (int i = 1; i <= M; i++) {
		scanf("%s%s", s, t);
		int u = G[s], v = G[t];
		D[u][v] = (int)(getDistance(lati[u], loti[u], lati[v], loti[v]) + 0.5);
	}
}

void Floyd () {
	for (int k = 1; k <= N; k++) {
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++)
				D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
		}
	}
}

int main () {
	int cas = 0;
	while (scanf("%d%d%d", &N, &M, &Q) == 3 && N+M+Q) {
		init();

		Floyd();
		if (cas) printf("\n");
		printf("Case #%d\n", ++cas);
		for (int i = 1; i <= Q; i++) {
			scanf("%s%s", s, t);
			int u = G[s], v = G[t];
			if (D[u][v] == inf) {
				printf("no route exists\n");
			} else
				printf("%d km\n", D[u][v]);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值