UVA 10075 Airlines

至此~UVA 计算几何分类下的  Great-Circle Distance (5/5) 5道题已经全部搞定。

全是关于大地坐标转换的问题。


这个题是给你大地坐标的城市以及航班,求最短路。

最短路用folyd即可。有个坑姐的 trick ,先前计算出来的长度也要四舍五入后再参与计算floyd = =。。

因为写floyd的三重循环。。开始觉得 for 循环好长啊。。以后开始用宏~

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")
#define FOR(i,s,n) for(int i=s; i<n; i++)
using namespace std;

const int MAX = 105;
const double r = 6378;
const double pi = 3.141592653589793;
const double inf = 1e30;
struct NODE{ double lng,lat;};
NODE p[MAX];
double air[MAX][MAX];

map<string, int> m;

double dis_3d(double lng1, double lat1, double lng2, double lat2)  
{           //经度,纬度,经度,纬度   
	double c = acos(cos(lat1)*cos(lat2)*cos(lng1 - lng2) + sin(lat1)*sin(lat2));   
 	double d = r * c; 
 	return d;
}

void Floyd(int n)
{
	FOR(i, 0, n)
	FOR(k, 0, n)
	FOR(j, 0, n)
		if( air[k][i] != inf && air[i][j] != inf
			&& air[k][i] + air[i][j] < air[k][j] )
			air[k][j] = air[k][i] + air[i][j];
}
		
int main()
{
	int n, M, q, ind = 1;
	bool f = false;
	string s, s1, s2;
		
	while( cin >> n >> M >> q )
	{
		m.clear();
	
		if( !n && !M && !q ) break; 
	
		if( f ) cout << endl;
		f = true; 	
		FOR(i, 0, n)
		{
			for(int k=0; k<n; k++)
				air[i][k] = inf;
			air[i][i] = 0;
		}
		
		FOR(i, 0, n)
		{
			cin >> s >> p[i].lat >> p[i].lng;
			m.insert( make_pair(s, i) );
		}
		
		while( M-- )
		{
			cin >> s1 >> s2;
			int a = m.find(s1)->second;
			int b = m.find(s2)->second;
			double len = dis_3d(p[a].lng*pi/180, p[a].lat*pi/180, 
							   	p[b].lng*pi/180, p[b].lat*pi/180);
			air[a][b] = min(air[a][b], floor(len+0.5));
		}
		
		Floyd(n);
		
		cout << "Case #" << ind++ << endl;
		
		while( q-- )
		{
			cin >> s1 >> s2;
			int a = m.find(s1)->second;
			int b = m.find(s2)->second;
			if( air[a][b] == inf )
				cout << "no route exists" << endl;
			else
				cout << air[a][b] << " km" << endl;
		}

	}

return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值