Codeforces Gym-101116-F (Flight Plan)

题目链接

Gym-101116

题目大意

假设地球是一个球体,给出起点的经纬度 A(x1,y1), 终点的经纬度 B(x2,y2)
x: 纬度,北纬为正,南纬为负;
y: 经度,东经为正,西经为负。
令:
D1 为两点的距离;
D2 为起点 A 先沿着纬线,再沿着经线 走到终点B的距离。
D1,D2

数据范围

1T10000
x1,x290,y1,y2180

解题思路

这道题不重要,重要的是怎么求地球上两点的距离。

图的话,将就看这个吧!懒得画了,,,只看图就好,,,
这里写图片描述

球面坐标和直角坐标存在一一对应的关系:球面有一点 M(r,φ,θ)
这里写图片描述
则,点 M 的直角坐标和球面坐标的关系为:

x=rcosφcosθy=rcosφsinθz=rsinφ

A(r,α,β),B(r,φ,θ)
OA=(rcosαcosβ,rcosαsinβ,rsinα),OB=(rcosφcosθ,rcosφsinθ,rsinφ)
由此可得,夹角 ang=arccos(OAOBOAOB)

化简得: ang=arccos(cosαcosφcos(βθ)+sinαsinφ)
夹角出来了,距离就好办了。

代码:

//化为弧度制
double rad(double d) {
    return d * PI / 180.0;
}

//求两点之间的距离
double dis(Point A, Point B) {
    A.x = rad(A.x); A.y = rad(A.y);
    B.x = rad(B.x); B.y = rad(B.y);
    double ang = acos(cos(A.x) * cos(B.x) * cos(A.y - B.y) + sin(A.x) * sin(B.x));
    double s = ang * R;    //R为地球半径
    return s;
}

我还看到一个公式,但我不知道怎么推出来的,若有好心大佬,求解答。

double dis(Point A, Point B) {
    double Theta = A.x - B.x;
    double Beta = A.y - B.y;
    double ang = 2 * asin(sqrt(sin(Theta / 2) * sin(Theta / 2) + cos(A.x) * cos(B.x) * sin(Beta / 2) * sin(Beta / 2)));
    double s = ang * R;
    return s;
}



距离的问题解决了,其他都不重要了。不对,这道题还没完呢!,,,
有一个坑点:当一个点在东半球,另一个在西半球时,需要特别注意一下角度,稍微想想。

AC代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <iostream>
using namespace std;
typedef long long LL;
const int inf = 1 << 30;
const LL INF = 1LL << 60;
const double PI = acos(-1.0);
const double R = 6371.0;

int T;
struct Point {
    double x, y;
};
Point a, b, c;

//化为弧度制
double rad(double d) {
    return d * PI / 180.0;
}

//求弧长
double arc(double Theta, double r) {
    return Theta * r;
}

//求两点之间的距离
double dis(Point A, Point B) {
    A.x = rad(A.x); A.y = rad(A.y);
    B.x = rad(B.x); B.y = rad(B.y);
    double ang = acos(cos(A.x) * cos(B.x) * cos(A.y - B.y) + sin(A.x) * sin(B.x));

    /*这个公式也可以,但我不知道是怎么推的
    double Theta = A.x - B.x;
    double Beta = A.y - B.y;
    double ang = 2 * asin(sqrt(sin(Theta / 2) * sin(Theta / 2) + cos(A.x) * cos(B.x) * sin(Beta / 2) * sin(Beta / 2)));
    */
    double s = ang * R;
    return s;
}

int main()
{
    scanf("%d", &T);
    while(T--) {
        scanf("%lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y);
        //这个稍微想想就明白了,一个小坑点
        if(abs(a.y - b.y) > 180.0) {
            if(a.y < 0.0) a.y += 360.0;
            else if(b.y < 0.0) b.y += 360.0;
        }
        c.x = a.x; c.y = b.y;
        double r = R * cos(rad(a.x));
        printf("%.10lf %.10lf\n", dis(a, b), dis(b, c) + arc(rad(abs(a.y - b.y)), r));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值