蛮水的题,给你N个城市的大地坐标,然后询问。
我查询用map了,应该会快一点哈~好方便滴说。
其他没啥了,就求下距离。
zoj poj AC的代码是先求角度,然后 ang*r是答案。
结果捏 UVA 死活过不去,后来看UVA的discuss,看了人家求距离的函数,贴了过来,过了 = = 。。
应该我的先求角度再求距离的方法被卡精度了。
贴上UVA AC的代码。
#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!!!")
using namespace std;
const int MAX = 101;
const double r = 6378;
const double pi = 3.141592653589793;
struct NODE{ double lng,lat;};
NODE p[MAX];
map<string, int> m;
double dis_3d(double lng1, double lat1, double lng2, double lat2)
{ //经度,纬度,经度,纬度
double dlon = lng2 - lng1;
double dlat = lat2 - lat1;
double a = pow((sin(dlat/2)),2) + cos(lat1) * cos(lat2) * pow(sin(dlon/2), 2);
double c = 2 * atan2(sqrt(a), sqrt(1-a));
double d = r * c;
return d;
}
int main()
{
int i = 0;
string s, s1, s2;
while( cin >> s )
{
if( s == "#" ) break;
cin >> p[i].lat >> p[i].lng;
m.insert( make_pair(s, i) );
i++;
}
while( cin >> s1 >> s2 )
{
if( s1 == "#" && s2 == "#" ) break;
if( !m.count(s1) || !m.count(s2) )
{
cout << s1 << " - " << s2 << endl;
cout << "Unknown" << endl;
continue;
}
int a = m.find(s1)->second;
int b = m.find(s2)->second;
double dis = dis_3d(p[a].lng*pi/180, p[a].lat*pi/180,
p[b].lng*pi/180, p[b].lat*pi/180);
dis = floor(dis + 0.5);
cout << s1 << " - " << s2 << endl;
cout << dis << " km" << endl;
}
return 0;
}