至此~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;
}