POJ 2254 Globetrotter (计算几何 - 球面最短距离)
题目链接:POJ 2254
Description
As a member of an ACM programming team you'll soon find yourself always traveling around the world: Zürich, Philadelphia, San José, Atlanta,... from 1999 on the Contest Finals even will be on a different continent each year, so one day you might get to Japan or Australia.
At the contest site it would be interesting to know how many miles you are away from home. For this sake, your job is to write a program to compute the geographical distance between two given locations on the Earth's surface.
We assume that the Earth is a perfect sphere with a radius of exactly 6378 km. The geographical distance between A and B is the length of the geodetic line segment connecting A and B.
The geodetic line segment between two points on a sphere is the shortest connecting curve lying entirely in the surface of the sphere.
The value of pi is approximately 3.141592653589793.
Input
The input will consist of two parts: a list of cities and a list of queries.
City List
The city list consists of up to 100 lines, one line per city. Each line will contain a string ci and two real numbers lati and longi, representing the city name, its latitude and its longitude, respectively.
The city name will be shorter than 30 characters and will not contain white-space characters.
The latitude will be between -90 (South Pole) and +90 (North Pole). The longitude will be between -180 and +180 where negative numbers denote locations west of the meridian and positive numbers denote locations east of the meridian. (The meridian passes through Greenwich, London.)
The city list will be terminated by a line consisting of a single "#".
Query List
Each line will contain two city names A and B.
The query list will be terminated by the line "# #".
Output
For each query, print a line saying "A - B" where A and B are replaced by the city names. Then print a line saying x km" where x is replaced by the geographical distance (in km) between the two cities, rounded to the nearest integer.
If one of the cities in the query didn't occur in the city list, print a line saying "Unknown" instead. Print a blank line after each query.
Sample Input
Ulm 48.700 10.500
Freiburg 47.700 9.500
Philadelphia 39.883 -75.250
SanJose 37.366 -121.933
NorthPole 90 0
SouthPole -90 0
#
Ulm Philadelphia
Ulm SanJose
Freiburg Philadelphia
Freiburg SanJose
Ulm Freiburg
SanJose Philadelphia
Ulm LasVegas
Ulm Ulm
Ulm NorthPole
Ulm SouthPole
NorthPole SouthPole
# #
Sample Output
Ulm - Philadelphia
6536 km
Ulm - SanJose
9367 km
Freiburg - Philadelphia
6519 km
Freiburg - SanJose
9412 km
Ulm - Freiburg
134 km
SanJose - Philadelphia
4023 km
Ulm - LasVegas
Unknown
Ulm - Ulm
0 km
Ulm - NorthPole
4597 km
Ulm - SouthPole
15440 km
NorthPole - SouthPole
20037 km
Source
Solution
题意
给定一些城市的经纬度,然后给出若干个询问,每个询问包含两个城市,求这两个城市的球面最短距离。
思路
已知两点经纬度求球面最短距离的公式:
\(AB = R\cdot arccos(cos(wA)cos(wB)cos(jB-jA)+sin(wA)sin(wB))\)
其中 \(wA\) 和 \(jA\) 代表 \(A\) 的纬度和经度,\(wB\) 和 \(jB\) 代表 \(B\) 的纬度和经度。
Code
#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
typedef double db;
const db pi = 3.141592653589793;
const db r = 6378.0;
class Point {
public:
double j, w;
Point(double j = 0, double w = 0) : j(j), w(w) {}
void input() {
scanf("%lf%lf", &w, &j);
w = w * pi / 180.0;
j = j * pi / 180.0;
}
db dis(Point a) {
return r * acos(cos(w) * cos(a.w) * cos(a.j - j) + sin(w) * sin(a.w));
}
};
map<string, Point> mp;
int main() {
string s1, s2;
while((cin >> s1) && s1[0] != '#') {
Point tmp;
tmp.input();
mp[s1] = tmp;
}
Point p1, p2;
while(cin >> s1 >> s2) {
if(s1[0] == '#' && s2[0] == '#') {
break;
}
cout << s1 << " - " << s2 << endl;
if(mp.find(s1) != mp.end() && mp.find(s2) != mp.end()) {
p1 = mp[s1], p2 = mp[s2];
double ans = p1.dis(p2);
printf("%.0lf km\n\n", ans);
} else {
printf("Unknown\n\n");
}
}
return 0;
}
POJ 2254 Globetrotter (计算几何 - 球面最短距离)的更多相关文章
- POJ 1410 Intersection (计算几何)
题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...
- POJ 1106 Transmitters(计算几何)
题目链接 切计算几何,感觉计算几何的算法还不熟.此题,枚举线段和圆点的直线,平分一个圆 #include <iostream> #include <cstring> #incl ...
- poj 2507Crossed ladders <计算几何>
链接:http://poj.org/problem?id=2507 题意:哪个直角三角形,一直角边重合, 斜边分别为 X, Y, 两斜边交点高为 C , 求重合的直角边长度~ 思路: 设两个三角形不重 ...
- TOYS - POJ 2318(计算几何,叉积判断)
题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数. 分析:做的第一道计算几何题目....使用叉积判断方 ...
- POJ 1654 Area 计算几何
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> usi ...
- A - TOYS(POJ - 2318) 计算几何的一道基础题
Calculate the number of toys that land in each bin of a partitioned toy box. 计算每一个玩具箱里面玩具的数量 Mom and ...
- hdu 4454 Stealing a Cake(计算几何:最短距离、枚举/三分)
题意:已知起点.圆.矩形,要求计算从起点开始,经过圆(和圆上任一点接触即可),到达矩形的路径的最短距离.(可以穿过园). 分析:没什么好的方法,凭感觉圆上的每个点对应最短距离,应该是一个凸函数,用三分 ...
- poj 2318 TOYS(计算几何 点与线段的关系)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12015 Accepted: 5792 Description ...
- POJ 3304 Segments(计算几何)
意甲冠军:给出的一些段的.问:能否找到一条直线,通过所有的行 思维:假设一条直线的存在,所以必须有该过两点的线,然后列举两点,然后推断是否存在与所有的行的交点可以是 代码: #include < ...
随机推荐
- xshell设置选中复制,右击粘贴功能
. 设置选中复制: 工具--->选项--->键盘和鼠标--->(然后根据下图设置保存即可) 2. 设置ctrl + v 粘贴功能: 工具--->选项--->键盘和鼠标-- ...
- 抓包工具charles下载安装(MAC版)
什么是charles? charles是一个HTTP代理服务器,HTTP监视器,反转代理服务器,当浏览器连接Charles的代理访问互联网时,Charles可以监控浏览器发送和接收的所有数据.它允许一 ...
- Android深度探索-卷1第二章心得体会
这章介绍了搭建Android开发环境的的搭建,主要是在Linux上搭建Android开发环境总体来说因为都是在Linux下开发的,so,只介绍了在Linux环境下的搭建在搭建过程中全是命令操作,和Wi ...
- Java并发AtomicLong接口
java.util.concurrent.atomic.AtomicLong类提供了可以被原子地读取和写入的底层long值的操作,并且还包含高级原子操作. AtomicLong支持基础long类型变量 ...
- javaIO流(三)--IO深入操作
一.字符编码 在计算机的世界中,本质上只认识0,1的字节数据,如果要想描述一些文字的编码就需要对这些二进制的数据进行组合,就需要对二进制的数据进行组合,所以才有了现在可看见的中文显示,但是在进行编码的 ...
- npm安装报错:Error: EACCES: permission denied
报错如下: sudo npm i webpack -g /Users/xesfe/.npm-global/bin/webpack -> /Users/xesfe/.npm-global/lib/ ...
- python2和python3 中 input()方法的不同之处
python2在input()获取输入值时,所获取的值等于本身的数据类型 a = input("请输入:") print(a,type(a)) #如果输入的时int行数字,a 获取 ...
- python第三方库安装失败处理方法
各位道友,是不是在使用pip 命令安装第三方库遇到了以下情形呢? 这种情况可真让人头疼啊..经过几番周折,终于找到了认为最有效的解决方法 首先 先把要安装的包下载下来,不管用什么方式 在这里我用的迅雷 ...
- 【转】vim 配置文件 ,高亮+自动缩进+行号+折叠+优化
将一下代码copy到 用户目录下 新建文件为 .vimrc保存即可生效: 如果想所有用户生效 请修改 /etc/vimrc (建议先cp一份) "===================== ...
- LOGO有哪几种常规设计思路?
Logo设计的思路多种多样,但是我个人从Logo设计的历史上,大致可以归纳出五种常规思路,思路的名称是自己编的,仅供大家参考.而列举的这些思路背后,都是有着各自的时代背景的. 先从历史最悠久的一种设计 ...