UVa Problem 10075 Airlines (航线)

本文介绍了一个结合球面距离计算与加权图最短路径问题的算法实现,详细阐述了如何通过Floyd-Warshall算法解决实际问题,并提供了关键代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// Airlines (航线)
// PC/UVa IDs: 111208/10075, Popularity: C, Success rate: high Level: 3
// Verdict: Accepted
// Submission Date: 2011-11-01
// UVa Run Time: 0.060s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// [解题方法]
// 该题把求球面上两点的曲面距离和加权图每对结点之间的最短路问题结合起来了。
// 该题的第一个关键是求球面上两点的距离,而计算球面上两点的距离是有固定的公式的,很方便求。求出了
// 曲面距离,则可以通过 Floyd-Warshall 算法计算任意点对之间的最短路径长度,从而可以顺利解决本题。
//
// 设点 p 的纬度和经度为(plat,plong),点 q 的纬度和经度为(qlat,qlong),地球的半径为 R,
// 则 p 和 q 之间的球面距离为(角度的单位为弧度):
//
// d(p, q) = R * arccos((sin(plat)sin(qlat) + cos(plat)cos(qlat)cos(plong − qlong))

#include <iostream>
#include <cmath>
#include <map>
#include <set>

using namespace std;

#define MAXN 100
#define MAXM 300
#define MAXQ 10000
#define PI 3.141592653589793
#define EARTH_RADIUS 6378
#define MAXINT (1L << 20)

int weight[MAXN + 1][MAXN + 1];
pair < double, double > locations[MAXN + 1];
map < string, int > cities;

// 将角度转换为弧度。
inline double radians(double degree)
{
	return (degree * 2.0 * PI / 360.0);
}

// 计算两个城市间航线的距离。
int calDistance(int start, int end)
{
	// 将角度转换为弧度。
	double plat = radians(locations[start].first);
	double plong = radians(locations[start].second);
	double qlat = radians(locations[end].first);
	double qlong = radians(locations[end].second);

	// 利用公式计算球面上两点的曲线距离。
	double distance = EARTH_RADIUS * acos(sin(plat) * sin(qlat) +
		cos(plat) * cos(qlat) * cos(plong - qlong));

	// 四舍五入取整并返回结果。
	return (int)(distance + 0.5);
}

int main(int ac, char *av[])
{
	int n, m, q, cases = 1;
	bool printSmartEmptyLine = false;
	string city, cityStart, cityEnd;
	double latitude, longitude;

	while (cin >> n >> m >> q, n || m || q)
	{
		// 初始化。
		cities.clear();
		for (int i = 1; i <= MAXN; i++)
			for (int j = 1; j <= MAXN; j++)
				weight[i][j] = MAXINT;

		// 读入城市和其坐标。
		for (int i = 1; i <= n; i++)
		{
			cin >> city >> latitude >> longitude;
			cities[city] = i;
			locations[i] = make_pair(latitude, longitude);
		}

		// 读入航线数据。
		for (int i = 1; i <= m; i++)
		{
			cin >> cityStart >> cityEnd;

			int idStart = cities[cityStart];
			int idEnd = cities[cityEnd];

			weight[idStart][idEnd] = calDistance(idStart, idEnd);
		}

		// 使用 Floyd-Warshall 算法计算任意两个城市间的最短航线距离。
		for (int k = 1; k <= n; k++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					weight[i][j] = min(weight[i][j],
						weight[i][k] + weight[k][j]);

		// 输出空行。
		if (printSmartEmptyLine)
			cout << endl;
		else
			printSmartEmptyLine = true;

		// 输出测试例数。
		cout << "Case #" << cases++ << endl;
		
		// 输出指定城市间的航线距离。
		for (int i = 1; i <= q; i++)
		{
			cin >> cityStart >> cityEnd;

			int idStart = cities[cityStart];
			int idEnd = cities[cityEnd];

			if (weight[idStart][idEnd] < MAXINT)
				cout << weight[idStart][idEnd] << " km" << endl;
			else
				cout << "no route exists" << endl;
		}
	}

	return 0;
}


### 使用云服务API创建航线 为了实现通过API接口创建航线的功能,通常需要遵循特定的开发流程和服务集成方式。当涉及到公有云环境下的模型部署与API调用时,可以考虑将训练好的模型部署到云端服务器,并利用RESTful API来提供对外的服务访问能力[^1]。 具体来说,在构建基于云原生技术栈的应用程序时,应当充分利用容器化、微服务架构以及声明式的API设计原则,这些特性有助于提高系统的可伸缩性和灵活性[^2]。 对于实际操作而言: 1. **准备阶段** - 确认所使用的云计算平台支持所需的API功能; - 安装必要的依赖组件如MQTT消息队列和对象存储MinIO等工具,这一步骤可以根据官方文档指导完成初始设置并保持默认配置以便快速启动项目[^3]; 2. **API请求结构** 假设目标是向某航空管理服务平台发送POST请求以新增一条航线记录,则可能涉及如下Python代码片段作为客户端发起HTTP POST请求的方式之一: ```python import requests url = 'https://api.example.com/airlines/routes' headers = {'Authorization': 'Bearer YOUR_ACCESS_TOKEN'} data = { "origin": "SHA", "destination": "PVG", "flight_number": "CA987" } response = requests.post(url, headers=headers, json=data) if response.status_code == 201: print('Route created successfully.') else: print(f'Failed to create route: {response.text}') ``` 上述示例展示了如何使用`requests`库构造并向指定URL地址提交包含新航线详情的数据包。需要注意的是,真实场景下还需处理身份验证机制(例如OAuth2)、错误码解析等内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值