跳到正文

题解

题解:P6840 [BalkanOI 2009] year2012 - A New Beginning

la_al约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年6月1日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 12 分钟
获赞
1
题目
P6840

版本

  1. v1当前

P6840 [BalkanOI 2009] year2012 - A New Beginning 解决方案

分析问题:

我们要在满足燃油约束的条件下,找到从起点机场到终点机场的最短时间路径。

以下是关键点:

  • 机场坐标在球面上,两点间的最短路径是球面距离(大圆距离)。
  • 飞机速度恒定,油箱容量固定。
  • 部分机场可加油(起点必可加油)。
  • 每条航线有特定的燃油消耗。
  • 起点机场初始满油,每次经过可加油机场时油箱会自动加满。
代码
          [地球]
         /     \
    [机场A]----[机场B] (可加油)
      |        |
    [机场C]  [机场D]
      |        |
    [起点]----[终点]

解决思路:带状态约束的 Dijkstra 算法

使用状态(节点, 剩余油量)表示当前位置:

  • 节点:机场索引(0-indexed)
  • 剩余油量:当前油箱中的燃油量(整数)

对于可加油机场,到达后油量重置为满(即状态变为(节点, C))

状态转移

从当前状态(u, fuel)出发:

  1. 遍历所有从u出发的边(u, v, time, cost_fuel)
  2. 检查当前油量是否足够:if (fuel >= cost_fuel)
  3. 计算新状态:
    • 新时间:new_time = current_time + time
    • 新油量:new_fuel = fuel - cost_fuel
    • 如果v可加油:new_fuel = C(油箱加满)。
  4. 如果新时间优于历史记录,更新状态并入队。

球面距离计算

由于所有机场到地心的距离相同(题目保证),我们使用向量点积计算夹角:

cos⁡θ=A⃗⋅B⃗∣A⃗∣⋅∣B⃗∣=A⃗⋅B⃗R02\cos \theta = \frac{\vec{A} \cdot \vec{B}}{|\vec{A}| \cdot |\vec{B}|} = \frac{\vec{A} \cdot \vec{B}}{R_0^2}

球面距离:distance = R0 * θ 飞行时间:time = distance / V

算法复杂度分析

状态数=N×(C+1)≈1000×1001=106时间复杂度=O(N⋅C⋅log⁡(N⋅C))≈106×log⁡2(106)≈106×20=2×107空间复杂度=O(N⋅C+M)≈106+104=1.01×106\text{状态数} = N \times (C + 1) \approx 1000 \times 1001 = 10^6 \\ \begin{aligned} \text{时间复杂度} &= O(N \cdot C \cdot \log(N \cdot C)) \\ &\approx 10^6 \times \log_2(10^6) \\ &\approx 10^6 \times 20 \\ &= 2 \times 10^7 \end{aligned} \begin{aligned} \text{空间复杂度} &= O(N \cdot C + M) \\ &\approx 10^6 + 10^4 \\ &= 1.01 \times 10^6 \end{aligned}

完整代码

C++
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1005; 
const int MAX_C = 1005; 
const double INF = 1e20;
const double EPS = 1e-9;

double dist[MAX_N][MAX_C];    // dist[u][f]: 到达机场u且剩余油量为f的最短时间
vector<tuple<int, double, int>> graph[MAX_N]; // 图结构: (邻接点, 飞行时间, 燃油消耗)
bool can_refuel[MAX_N];       // can_refuel[i]: 机场i是否可加油
double cx[MAX_N], cy[MAX_N], cz[MAX_N];

int main() {
    int n, m;       // 机场数, 航线数
    double velocity; // 飞机速度
    int capacity;   // 油箱容量
    scanf("%d %d %lf %d", &n, &m, &velocity, &capacity);

    for (int i = 0; i < n; i++) {
        scanf("%lf %lf %lf %d", &cx[i], &cy[i], &cz[i], &can_refuel[i]);
    }

    // 计算地球半径(所有机场到地心的距离相同)
    double earthRad = sqrt(cx[0]*cx[0] + 
                               cy[0]*cy[0] + 
                               cz[0]*cz[0]);

    // 构建航线图
    for (int i = 0; i < m; i++) {
        int a, b, fuel_cost;
        scanf("%d %d %d", &a, &b, &fuel_cost);
        a--; b--; // 转换为0-indexed

        // 计算向量点积
        double dot = cx[a]*cx[b] + 
                            cy[a]*cy[b] + 
                            cz[a]*cz[b];
        
        // 计算夹角余弦值,处理浮点精度问题
        double cos_theta = dot / (earthRad * earthRad);
        if (cos_theta > 1.0) cos_theta = 1.0;
        if (cos_theta < -1.0) cos_theta = -1.0;
        
        // 计算球面距离和飞行时间
        double theta = acos(cos_theta);
        double distance = earthRad * theta;
        double time = distance / velocity;

        // 添加双向边
        graph[a].push_back(make_tuple(b, time, fuel_cost));
        graph[b].push_back(make_tuple(a, time, fuel_cost));
    }

    // 读入起点和终点
    int start, target;
    scanf("%d %d", &start, &target);
    start--; target--; // 转换为0-indexed

    // 初始化距离数组为无穷大
    for (int i = 0; i < n; i++) {
        for (int f = 0; f <= capacity; f++) {
            dist[i][f] = INF;
        }
    }
    
    // 优先队列:按时间升序排列 (时间, 节点, 剩余油量)
    priority_queue<tuple<double, int, int>, 
                   vector<tuple<double, int, int>>, 
                   greater<tuple<double, int, int>>> pq;
    
    // 起点状态:时间0,油量满
    dist[start][capacity] = 0.0;
    pq.push(make_tuple(0.0, start, capacity));

    double min_time = -1; // 记录到达终点的最短时间

    while (!pq.empty()) {
        // 取出当前最短时间的状态
        auto [cur_time, cur_node, cur_fuel] = pq.top();
        pq.pop();
        
        // 如果当前状态不是最优,跳过
        if (fabs(cur_time - dist[cur_node][cur_fuel]) > EPS) 
            continue;
        
        // 到达终点,记录答案并退出
        if (cur_node == target) {
            min_time = cur_time;
            break;
        }
        
        // 遍历所有邻接边
        for (const auto& edge : graph[cur_node]) {
            int neighbor = get<0>(edge);      // 邻接机场
            double edge_time = get<1>(edge);  // 飞行时间
            int fuel_cost = get<2>(edge);     // 燃油消耗
            
            // 检查油量是否足够飞行
            if (cur_fuel < fuel_cost) 
                continue;
            
            // 计算新状态
            double new_time = cur_time + edge_time;
            int new_fuel = cur_fuel - fuel_cost;
            
            // 在可加油机场补充燃油
            if (can_refuel[neighbor]) {
                new_fuel = capacity;
            }
            
            // 如果找到更短时间,更新状态
            if (new_time < dist[neighbor][new_fuel]) {
                dist[neighbor][new_fuel] = new_time;
                pq.push(make_tuple(new_time, neighbor, new_fuel));
            }
        }
    }

    if (min_time < 0) {
        printf("0\n"); // 无法到达终点
    } else {
        printf("%.8lf\n", min_time); // 输出最短时间
    }

    return 0;
}

关键代码解释

1. 球面距离计算

C++
double dot = cx[a]*cx[b] + 
                    cy[a]*cy[b] + 
                    cz[a]*cz[b];
double cos_theta = dot / (earthRad * earthRad);
cos_theta = max(min(cos_theta, 1.0), -1.0); // 截断到[-1,1]区间
double theta = acos(cos_theta);
double distance = earth_radius * theta;

这段代码计算两点间的球面距离(大圆距离):

  1. 计算两坐标向量的点积。
  2. 根据点积计算夹角的余弦值。
  3. 处理浮点精度问题,确保余弦值在[-1,1]区间。
  4. 反余弦得到夹角。
  5. 计算弧长(球面距离)。

2. 状态转移的核心逻辑

C++
if (cur_fuel < fuel_cost) 
    continue; // 油量不足,跳过

double new_time = cur_time + edge_time;
int new_fuel = cur_fuel - fuel_cost;

if (can_refuel[neighbor]) {
    new_fuel = capacity; // 可加油机场,油箱加满
}

if (new_time < dist[neighbor][new_fuel]) {
    dist[neighbor][new_fuel] = new_time;
    pq.push(make_tuple(new_time, neighbor, new_fuel));
}

这段代码处理状态转移:

  1. 检查当前油量是否足够飞行该航线。
  2. 计算新状态的时间和剩余油量。
  3. 如果在可加油机场降落,油量重置为满。
  4. 如果新状态的时间更优,更新状态并入队。

3. Dijkstra 算法优先队列

C++
priority_queue<tuple<double, int, int>, 
               vector<tuple<double, int, int>>, 
               greater<tuple<double, int, int>>> pq;

使用最小堆实现的优先队列,确保每次处理时间最短的状态,这是算法的核心。

果然这种题对我而言还是太有难度了(做了4h),希望过审。

原文