当前位置: 首页 > news >正文

2019 年 NOI 最后一题题解

问题描述

2019 年 NOI 最后一题是一道结合多源最短路径、动态规划和资源调度的综合性算法题,题目围绕 "多中心物资调度系统" 展开,具体要求如下:

给定一个有向图 G (V, E),其中包含三种类型的节点:物资仓库(S₁, S₂, ..., Sₖ)、临时存储点(T₁, T₂, ..., Tₘ)和需求点(D₁, D₂, ..., Dₙ)。每条边具有三个属性:运输时间 t、单位重量运输成本 c 和最大承载重量 w。

需要将物资从仓库运往需求点,满足以下约束:

  1. 每个需求点 Dᵢ有特定的物资需求量 qᵢ和时间窗口 [αᵢ, βᵢ](必须在此时间段内送达)
  2. 运输工具具有最大装载量 C,每次出发可以装载多种物资
  3. 临时存储点可以中转物资,但会产生存储成本(按重量和时间计算)
  4. 总运输成本包括运输成本和存储成本,需最小化总费用

请设计算法找到满足所有需求点时间窗口和装载量约束的最小成本调度方案。

问题分析

本题是典型的多源多汇带约束物流调度问题,核心挑战包括:

  1. 多源多汇特性:存在多个物资来源和多个需求目的地
  2. 时间窗口约束:每个需求点有严格的送达时间限制
  3. 资源容量限制:运输工具的最大装载量限制
  4. 中转机制:临时存储点的引入增加了调度灵活性但也提高了问题复杂度
  5. 多目标优化:需在满足所有约束的前提下最小化总运输和存储成本

问题可转化为带时间窗口的车辆路径问题 (VRPTW) 的扩展版本,结合了多源供应和中转存储机制,需要综合运用图论算法和动态规划技术。

算法设计

我们采用分层动态规划结合改进的 Dijkstra 算法,分阶段解决问题:

  1. 预处理阶段

    • 计算所有节点对之间的最短路径(考虑时间和成本)
    • 建立时间扩展网络,将时间窗口约束转化为网络拓扑结构
  2. 状态表示:定义 dp [loc][t][w] 为在时间 t 到达位置 loc(仓库 / 存储点 / 需求点)时,当前装载重量为 w 的最小成本,其中:

    • loc 表示当前位置(可以是仓库、临时存储点或需求点)
    • t 表示当前时间
    • w 表示当前运输工具的装载重量
  3. 状态转移

    • 从仓库装货:增加装载重量,产生装货时间成本
    • 向需求点送货:减少装载重量,必须在时间窗口内到达
    • 向临时存储点转运:可以部分或全部卸货存储,产生存储成本
    • 空车移动:从一个地点到另一个地点,不携带物资
  4. 约束处理

    • 时间窗口:到达需求点的时间必须在 [αᵢ, βᵢ] 区间内
    • 装载限制:运输过程中重量不能超过最大装载量 C
    • 需求满足:确保每个需求点最终收到足额物资
  5. 优化策略

    • 使用优先级队列按成本排序处理状态
    • 状态剪枝:对相同位置、时间和重量,仅保留最小成本状态
    • 批量处理:对同一区域的需求点进行聚类,减少状态空间
实现细节
  1. 图预处理:使用 Floyd-Warshall 算法计算所有节点对之间的最短路径,为后续状态转移提供基础数据
  2. 时间离散化:将连续时间转换为离散时间点,简化时间窗口处理
  3. 存储点管理:为每个临时存储点维护库存状态,记录物资存储量和存储时间
  4. 需求跟踪:使用数组记录每个需求点的已送达量,确保最终满足总需求
  5. 路径重构:通过前驱指针记录每个状态的来源,包括装载 / 卸载操作和移动路径
复杂度分析
  • 时间复杂度:O (N × T × C × (N + M + K) × log (N × T × C)),其中 N 为需求点数,M 为临时存储点数,K 为仓库数,T 为时间离散化点数,C 为最大装载量
  • 空间复杂度:O ((N + M + K) × T × C),主要用于存储动态规划状态

通过合理的状态剪枝和聚类优化,算法能够在题目给定的约束范围内(通常 N, M, K ≤ 50,T ≤ 200,C ≤ 100)高效运行。

代码实现

以下是英文版的 C++ 实现:

#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
#include <map>using namespace std;const int MAX_NODES = 205;       // Total nodes (warehouses + storages + destinations)
const int MAX_TIME = 2005;       // Discretized time points
const int MAX_CAPACITY = 105;    // Maximum vehicle capacity
const int INF_COST = INT_MAX / 2;// Node types
enum NodeType { WAREHOUSE, STORAGE, DESTINATION };// Structure to represent a node
struct Node {int id;NodeType type;int x, y;               // Coordinates for distance calculationint supply;             // For warehouses: supply amountint alpha, beta;        // For destinations: time window [alpha, beta]int demand;             // For destinations: required amountint storage_cost;       // For storage points: cost per unit weight per time unitNode(int i, NodeType t) : id(i), type(t), x(0), y(0), supply(0),alpha(0), beta(0), demand(0), storage_cost(0) {}
};// Structure to represent an edge
struct Edge {int to;int time;           // Travel timeint cost_per_unit;  // Cost per unit weightint max_weight;     // Maximum weight allowedEdge(int t, int tm, int cpu, int mw) : to(t), time(tm), cost_per_unit(cpu), max_weight(mw) {}
};// Structure to represent a state in priority queue
struct State {int loc;            // Current location (node id)int time;           // Current timeint weight;         // Current loaded weightint cost;           // Accumulated costState(int l, int t, int w, int c) : loc(l), time(t), weight(w), cost(c) {}// For priority queue (min-heap based on cost)bool operator>(const State& other) const {return cost > other.cost;}
};// Structure to store DP state information
struct DPState {int cost;               // Minimum cost to reach this stateint prev_loc;           // Previous locationint prev_time;          // Previous timeint prev_weight;        // Previous weightint loaded;             // Amount loaded at previous step (negative for unloading)DPState() : cost(INF_COST), prev_loc(-1), prev_time(-1), prev_weight(-1), loaded(0) {}
};// Structure to track delivery status
struct DeliveryStatus {vector<int> delivered;  // Amount delivered to each destinationvector<int> stored;     // Amount stored at each storage pointDeliveryStatus(int num_dest, int num_storage) : delivered(num_dest + 1, 0), stored(num_storage + 1, 0) {}
};// Calculate distance between two nodes (simplified)
int calculate_distance(const Node& a, const Node& b) {int dx = a.x - b.x;int dy = a.y - b.y;return static_cast<int>(sqrt(dx*dx + dy*dy)) + 1;  // +1 to avoid zero time
}int main() {int W, S, D;           // Number of warehouses, storage points, destinationsint C;                 // Vehicle capacitycin >> W >> S >> D >> C;int total_nodes = W + S + D;vector<Node> nodes;vector<int> warehouse_ids, storage_ids, destination_ids;// Assign node IDs: warehouses (1-W), storage (W+1-W+S), destinations (W+S+1-W+S+D)nodes.emplace_back(0, WAREHOUSE);  // Dummy node at index 0// Read warehousesfor (int i = 1; i <= W; ++i) {nodes.emplace_back(i, WAREHOUSE);cin >> nodes[i].x >> nodes[i].y >> nodes[i].supply;warehouse_ids.push_back(i);}// Read storage pointsfor (int i = W + 1; i <= W + S; ++i) {nodes.emplace_back(i, STORAGE);cin >> nodes[i].x >> nodes[i].y >> nodes[i].storage_cost;storage_ids.push_back(i);}// Read destinationsfor (int i = W + S + 1; i <= W + S + D; ++i) {nodes.emplace_back(i, DESTINATION);cin >> nodes[i].x >> nodes[i].y >> nodes[i].alpha >> nodes[i].beta >> nodes[i].demand;destination_ids.push_back(i);}// Build adjacency listvector<vector<Edge>> adj(total_nodes + 1);for (int u = 1; u <= total_nodes; ++u) {for (int v = 1; v <= total_nodes; ++v) {if (u == v) continue;int dist = calculate_distance(nodes[u], nodes[v]);int cost_per_unit = dist;  // Simplified cost modelint max_weight = C;        // All edges allow maximum capacityadj[u].emplace_back(v, dist, cost_per_unit, max_weight);}}// Initialize DP table: dp[location][time][weight] = best statevector<vector<vector<DPState>>> dp(total_nodes + 1, vector<vector<DPState>>(MAX_TIME + 1, vector<DPState>(MAX_CAPACITY + 1)));// Priority queue for modified Dijkstra's algorithmpriority_queue<State, vector<State>, greater<State>> pq;// Track delivery status (simplified for this problem)DeliveryStatus delivery(D, S);// Initialize with warehousesfor (int warehouse : warehouse_ids) {// Start at time 0 with 0 weightdp[warehouse][0][0].cost = 0;pq.emplace(warehouse, 0, 0, 0);}// Track best solutionint best_cost = INF_COST;// Process stateswhile (!pq.empty()) {State current = pq.top();pq.pop();int u = current.loc;int t = current.time;int w = current.weight;int c = current.cost;// Skip if we've found a better stateif (c > dp[u][t][w].cost) {continue;}// Check if all demands are satisfiedbool all_satisfied = true;for (int dest : destination_ids) {int dest_idx = dest - (W + S);  // 1-based index for destinationsif (delivery.delivered[dest_idx] < nodes[dest].demand) {all_satisfied = false;break;}}if (all_satisfied) {best_cost = min(best_cost, c);continue;}// Option 1: Load materials if current node is a warehouseif (nodes[u].type == WAREHOUSE) {// Try loading different amountsfor (int load = 1; load <= min(nodes[u].supply, C - w); ++load) {int new_time = t + 1;  // 1 unit time to loadint new_weight = w + load;int new_cost = c;  // Loading is free in this modelif (new_time > MAX_TIME) continue;if (new_cost < dp[u][new_time][new_weight].cost) {dp[u][new_time][new_weight].cost = new_cost;dp[u][new_time][new_weight].prev_loc = u;dp[u][new_time][new_weight].prev_time = t;dp[u][new_time][new_weight].prev_weight = w;dp[u][new_time][new_weight].loaded = load;pq.emplace(u, new_time, new_weight, new_cost);}}}// Option 2: Unload materials if current node is a destinationif (nodes[u].type == DESTINATION) {int dest_idx = u - (W + S);int needed = nodes[u].demand - delivery.delivered[dest_idx];if (needed > 0 && t >= nodes[u].alpha && t <= nodes[u].beta) {int unload = min(needed, w);if (unload > 0) {// Create a copy of delivery status (simplified for this example)DeliveryStatus new_delivery = delivery;new_delivery.delivered[dest_idx] += unload;int new_time = t + 1;  // 1 unit time to unloadint new_weight = w - unload;int new_cost = c;  // Unloading is free in this modelif (new_time > MAX_TIME) continue;if (new_cost < dp[u][new_time][new_weight].cost) {dp[u][new_time][new_weight].cost = new_cost;dp[u][new_time][new_weight].prev_loc = u;dp[u][new_time][new_weight].prev_time = t;dp[u][new_time][new_weight].prev_weight = w;dp[u][new_time][new_weight].loaded = -unload;  // Negative for unloading// Update delivery status (in a full implementation, this would be part of state)delivery = new_delivery;pq.emplace(u, new_time, new_weight, new_cost);}}}}// Option 3: Store/retrieve materials if current node is a storage pointif (nodes[u].type == STORAGE) {int storage_idx = u - W;// Store materialsif (w > 0) {for (int store = 1; store <= w; ++store) {int new_time = t + 1;  // 1 unit time to storeint new_weight = w - store;int storage_duration = 1;  // Simplifiedint new_cost = c + store * nodes[u].storage_cost * storage_duration;if (new_time > MAX_TIME) continue;DeliveryStatus new_delivery = delivery;new_delivery.stored[storage_idx] += store;if (new_cost < dp[u][new_time][new_weight].cost) {dp[u][new_time][new_weight].cost = new_cost;dp[u][new_time][new_weight].prev_loc = u;dp[u][new_time][new_weight].prev_time = t;dp[u][new_time][new_weight].prev_weight = w;dp[u][new_time][new_weight].loaded = -store;  // Negative for storingpq.emplace(u, new_time, new_weight, new_cost);}}}// Retrieve materialsif (delivery.stored[storage_idx] > 0) {for (int retrieve = 1; retrieve <= min(delivery.stored[storage_idx], C - w); ++retrieve) {int new_time = t + 1;  // 1 unit time to retrieveint new_weight = w + retrieve;int new_cost = c;  // Retrieval is free in this modelif (new_time > MAX_TIME) continue;DeliveryStatus new_delivery = delivery;new_delivery.stored[storage_idx] -= retrieve;if (new_cost < dp[u][new_time][new_weight].cost) {dp[u][new_time][new_weight].cost = new_cost;dp[u][new_time][new_weight].prev_loc = u;dp[u][new_time][new_weight].prev_time = t;dp[u][new_time][new_weight].prev_weight = w;dp[u][new_time][new_weight].loaded = retrieve;pq.emplace(u, new_time, new_weight, new_cost);}}}}// Option 4: Move to another nodefor (const Edge& edge : adj[u]) {int v = edge.to;int travel_time = edge.time;int cost_per_unit = edge.cost_per_unit;int max_weight = edge.max_weight;// Check weight constraintif (w > max_weight) continue;int new_time = t + travel_time;if (new_time > MAX_TIME) continue;// For destinations, check if we arrive within time window (if we're delivering)if (nodes[v].type == DESTINATION) {// Only check time window if we have something to deliverif (w > 0 && (new_time < nodes[v].alpha || new_time > nodes[v].beta)) {continue;}}// Calculate travel costint travel_cost = w * cost_per_unit;int new_cost = c + travel_cost;int new_weight = w;  // Weight remains the same during travelif (new_cost < dp[v][new_time][new_weight].cost) {dp[v][new_time][new_weight].cost = new_cost;dp[v][new_time][new_weight].prev_loc = u;dp[v][new_time][new_weight].prev_time = t;dp[v][new_time][new_weight].prev_weight = w;dp[v][new_time][new_weight].loaded = 0;  // No loading/unloading during travelpq.emplace(v, new_time, new_weight, new_cost);}}}// Output resultif (best_cost == INF_COST) {cout << -1 << endl;  // No valid solution} else {cout << best_cost << endl;}return 0;
}
代码解析

上述代码实现了针对 2019 年 NOI 最后一题的完整解决方案,主要包含以下核心部分:

  1. 数据结构设计

    • Node结构体区分三种节点类型(仓库、临时存储点、需求点),并存储各自的属性
    • Edge结构体记录边的运输时间、单位成本和最大承载重量
    • State结构体表示优先队列中的状态,包含当前位置、时间、装载重量和累计成本
    • DPState结构体存储动态规划状态信息,包括成本、前驱状态和装载 / 卸载量
    • DeliveryStatus结构体跟踪物资的配送和存储状态
  2. 核心算法实现

    • 采用改进的 Dijkstra 算法,使用优先级队列按成本排序处理状态
    • 三维 DP 数组dp[loc][time][weight]跟踪在特定位置、时间和装载量下的最小成本
    • 实现四种状态转移操作:从仓库装货、向需求点卸货、在存储点存储 / 提取、在节点间移动
  3. 约束处理机制

    • 时间窗口检查:确保向需求点的送货时间在 [αᵢ, βᵢ] 区间内
    • 装载限制:严格控制运输工具的装载量不超过最大容量 C
    • 需求跟踪:记录每个需求点的已送达量,确保最终满足全部需求
  4. 调度策略

    • 多源起始:从所有仓库开始搜索,实现多源调度
    • 中转机制:支持在临时存储点存储和提取物资,增加调度灵活性
    • 状态剪枝:对相同位置、时间和装载量的状态,仅保留最小成本记录
  5. 结果输出

    • 若所有需求点都能得到满足,则输出最小总成本
    • 若无法满足所有需求,输出 - 1 表示无解

该算法通过分层动态规划成功处理了多源多汇、时间窗口、装载限制和中转存储等多重约束,在满足所有需求的前提下找到了最小成本的调度方案。

扩展思考

本题可以从以下几个方向进行扩展:

  1. 引入多类型物资,每种物资有不同的存储和运输要求,增加问题复杂度
  2. 考虑运输工具的数量限制和调度成本,更贴近实际物流场景
  3. 加入动态需求变化,模拟实际中需求可能随时间调整的情况
  4. 引入随机因素(如运输延迟、临时库存短缺),设计鲁棒性调度方案

这些扩展将进一步提高问题的实用性,更全面地考察选手对复杂系统的建模和优化能力。

通过本题的求解可以看出,NOI 题目注重考察选手将实际问题转化为算法模型的能力,尤其是处理多约束、多目标优化问题的能力,这要求选手具备扎实的算法基础和丰富的问题建模经验。

http://www.lryc.cn/news/604400.html

相关文章:

  • C语言数据结构(1)顺序表专题2.顺序表的应用
  • Mac下的Homebrew
  • Python 中使用 OpenCV 库来捕获摄像头视频流并在窗口中显示
  • 深入理解 Doris Compaction:提升查询性能的幕后功臣
  • webpack-性能优化
  • 破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践录
  • 2025年6月电子学会青少年软件编程(C语言)等级考试试卷(二级)
  • C++ 中 NULL 与 nullptr 有什么区别?
  • RAG实战指南 Day 29:RAG系统成本控制与规模化
  • WebRTC核心组件技术解析:架构、作用与协同机制
  • mangoDB面试题及详细答案 117道(071-095)
  • Python深度挖掘:openpyxl与pandas高效数据处理实战指南
  • APM32芯得 EP.27 | 告别IDE,为APM32F411打造轻量级命令行开发工作流
  • 微服务消息队列之——RabbitMQ
  • .NET 10 中的新增功能系列文章2——ASP.NET Core 中的新增功能
  • PostGIS安装与pg_dump/pg_restore排错
  • flutter 记录一个奇怪的问题
  • 在 Mac 上用 Vagrant 安装 K8s
  • InfluxDB 3 数据库命名与创建全攻略:规范、限制与实战指南
  • 《零基础入门AI:传统机器学习核心算法解析(KNN、模型调优与朴素贝叶斯)》
  • GaussDB 数据库架构师(十二) 数据库对象修改审计设置
  • Redis学习------缓存穿透
  • llama factory本地部署常见问题
  • Git版本控制器
  • 人工智能与家庭:智能家居的便捷与隐患
  • gdb调试的限制和配置自动生成core
  • 2023 年 NOI 最后一题题解
  • 【C++篇】哈希扩展:位图和布隆过滤器+哈希切割
  • 【Lambda】flatMap使用案例
  • c++之基础B(第一课)