The following problem is solved by branch and bound method and the C++ code is given Li wants to travel around the country by car. He finds that the price of gasoline varies from city to city during the trip. It is obvious that if he can adopt a reasonable way to fill up, he will save the cost of the whole trip. Assuming the car starts with an empty tank, it will use one unit of gas for every unit of distance it travels. Please help Xiao Li find the most economical way to complete his entire road trip. Input requirements: The first line of the input contains two integers n (1 ≤ n ≤ 1000) and m (0 ≤ m ≤ 10000), which represent the number of cities and the number of roads, respectively. The following line contains n integers, each representing the price of gasoline in n cities. The following m lines, each containing three integers u,v,d, represent the city u, and the distance between v is d (1 ≤ d ≤ 100). The last line contains three integers c (1 ≤ c ≤ 100),s, and e, which represent the capacity of the tank, the city of departure, and the city of last arrival, respectively. Output requirements: If it is possible to help Xiao Li find a route from s to e that is most economical, the corresponding cost will be output; otherwise, "impossible" will be output. Sample input: 5 5 10, 10, 20, 12, 13 0, 1, 9 0, 2, 8 One, two, one 1, 3, 11 Two, three, seven Ten zero three Sample output: 170

时间: 2024-02-14 21:16:59 浏览: 23
以下是使用分支界限法解决该问题的C++代码: ```c++ #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int N = 1010, M = 20010; int n, m, c, s, e; int p[N], h[N], e[M], ne[M], w[M], idx; int dist[N][110]; // dist[i][j]表示从起点到i,油量为j时的最短路 bool st[N][110]; struct Node { int u, d, c; bool operator< (const Node& t) const { return d > t.d; } }; void add(int a, int b, int c) { e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++; } int dijkstra() { memset(dist, 0x3f, sizeof dist); priority_queue<Node> heap; heap.push({s, 0, 0}); dist[s][0] = 0; while (heap.size()) { auto t = heap.top(); heap.pop(); int u = t.u, d = t.d, c = t.c; if (st[u][c]) continue; st[u][c] = true; for (int i = h[u]; ~i; i = ne[i]) { int j = e[i], v = w[i]; if (c + j <= c && dist[j][c + j] > dist[u][c] + p[j] * (c + j - d)) { dist[j][c + j] = dist[u][c] + p[j] * (c + j - d); heap.push({j, dist[j][c + j], c + j}); } } } int res = 0x3f3f3f3f; for (int i = 0; i <= c; i ++ ) res = min(res, dist[e][i]); if (res == 0x3f3f3f3f) return -1; return res; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i ++ ) scanf("%d", &p[i]); memset(h, -1, sizeof h); while (m -- ) { int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b, c), add(b, a, c); } scanf("%d%d%d", &c, &s, &e); int res = dijkstra(); if (res == -1) puts("impossible"); else printf("%d\n", res); return 0; } ``` 思路解析: - 由于题目要求求最优解,因此我们可以想到使用单源最短路径算法来求解,这里我们采用Dijkstra算法。 - 由于油量对于最短路的计算有影响,因此我们可以使用二维数组`dist[i][j]`来表示从起点到i,油量为j时的最短路。 - 在Dijkstra算法中,每次从堆中取出距离起点最近的节点`u`,在遍历`u`的邻居节点`v`时,我们需要考虑两种情况:(1)当前油量`c`可以到达节点`v`,则直接更新`dist[v][c]`;(2)当前油量`c`无法到达节点`v`,则需要选择一个新的油量`c'`来到达节点`v`,那么节点`v`的油量将变为`c' - dis(u, v)`,因此我们可以将更新`dist[v][c']`的过程转化为更新`dist[v][c]`的过程,即`dist[v][c'] = dist[u][c] + p[v] * (c' - dis(u, v))`,其中`p[v]`表示节点`v`的油价。 - 由于油量是一个连续的值,因此我们需要使用bool数组`st[i][j]`来标记节点`i`的油量是否被访问过,防止重复访问。 - 最后,我们在`dist[e][i]`中取最小值即为答案。 时间复杂度:$O(mc\log n)$ 其中,$m$为边数,$c$为油量,$n$为点数。

相关推荐

From Proposition 1, we plug ri,O = li(μ)τi into (39) and rewrite problem (38) as maximize ri,O 􏰗ai − μ li (μ) − Yi(t)g [li(μ)]􏰘 ri,O li (μ)hi (41a) March 2, 2021 DRAFT maximize ˆr O subject to 0 ≤ ri,O ≤ Qi(t), (41b) 0, ifa − μ −Y(t)g[li(μ)] <0, subject to where the optimal solution is r∗ i,O Accordingly, we have τ∗ = r∗ ii,Oi i1 of μ in (32) as 1−􏰀i∈M1 τi∗. Then, we obtain the optimal dual variable μ∗ through the ellipsoid method (bi-section search in this case) over the range [0,∆], where ∆ is a sufficiently large value, until a prescribed precision requirement is met. Given the optimal μ∗, we denote the optimal ratio obtained from (40) as li (μ∗) 􏰝 r∗ /τ∗, i,O i ∀i ∈ M1. Notice that the optimal solution 􏰕τi∗, r∗ , ∀i ∈ M1􏰖 of the dual problem may not be i,O primal feasible. Therefore, to find a primal optimal solution to (31), we substitute τi = ri,O/li (μ∗) into (31) and simplify the problem as = i li(μ) i li(μ)hi (42) otherwise. Qi (t), /l (μ). After obtaining τ∗, ∀i ∈ M , we calculate the subgradient 􏰁 􏰗ai − Yi(t)g [li(μ∗)]􏰘 ri,O (43a) i ∈ M 1 h i l i ( μ ∗ ) 􏰁 ri,O ≤ 1, ri,O ≤ Qi(t), ∀i ∈ M1. (43b) i∈M1 li(μ∗) The above problem is a simple linear programming (LP) that can be easily solved. With a bit abuse of notation, we denote the optimal solution of (43) as ˆr∗ = 􏰕r∗ , ∀i ∈ M 􏰖 and retrieve 20 the optimal solution to (31) as τ∗=r∗ /l(μ∗),e∗ =τi∗g[li(μ∗)],∀i∈M. (44) i i,O i i,O hili(μ∗) 1 Denote τˆ∗ = {τi∗,∀i ∈ M1} and ˆe∗O = 􏰕e∗i,O,∀i ∈ M1􏰖. As {τˆ∗,ˆe∗O,ˆr∗O,μ∗} satisfies the KKT conditions, {τˆ∗,ˆe∗O,ˆr∗O} is an optimal solution to (31). By combining the optimal solutions in (30) and (44), we obtain an optimal solution of (P4). We summarize the pseudo-code of the O i,O 1 algorithm to solve (P4) in Algorithm 2.,翻译并解释li和hi是什么

Algorithm 1: The online LyDROO algorithm for solving (P1). input : Parameters V , {γi, ci}Ni=1, K, training interval δT , Mt update interval δM ; output: Control actions 􏰕xt,yt􏰖Kt=1; 1 Initialize the DNN with random parameters θ1 and empty replay memory, M1 ← 2N; 2 Empty initial data queue Qi(1) = 0 and energy queue Yi(1) = 0, for i = 1,··· ,N; 3 fort=1,2,...,Kdo 4 Observe the input ξt = 􏰕ht, Qi(t), Yi(t)􏰖Ni=1 and update Mt using (8) if mod (t, δM ) = 0; 5 Generate a relaxed offloading action xˆt = Πθt 􏰅ξt􏰆 with the DNN; 6 Quantize xˆt into Mt binary actions 􏰕xti|i = 1, · · · , Mt􏰖 using the NOP method; 7 Compute G􏰅xti,ξt􏰆 by optimizing resource allocation yit in (P2) for each xti; 8 Select the best solution xt = arg max G 􏰅xti , ξt 􏰆 and execute the joint action 􏰅xt , yt 􏰆; { x ti } 9 Update the replay memory by adding (ξt,xt); 10 if mod (t, δT ) = 0 then 11 Uniformly sample a batch of data set {(ξτ , xτ ) | τ ∈ St } from the memory; 12 Train the DNN with {(ξτ , xτ ) | τ ∈ St} and update θt using the Adam algorithm; 13 end 14 t ← t + 1; 15 Update {Qi(t),Yi(t)}N based on 􏰅xt−1,yt−1􏰆 and data arrival observation 􏰙At−1􏰚N using (5) and (7). i=1 i i=1 16 end With the above actor-critic-update loop, the DNN consistently learns from the best and most recent state-action pairs, leading to a better policy πθt that gradually approximates the optimal mapping to solve (P3). We summarize the pseudo-code of LyDROO in Algorithm 1, where the major computational complexity is in line 7 that computes G􏰅xti,ξt􏰆 by solving the optimal resource allocation problems. This in fact indicates that the proposed LyDROO algorithm can be extended to solve (P1) when considering a general non-decreasing concave utility U (rit) in the objective, because the per-frame resource allocation problem to compute G􏰅xti,ξt􏰆 is a convex problem that can be efficiently solved, where the detailed analysis is omitted. In the next subsection, we propose a low-complexity algorithm to obtain G 􏰅xti, ξt􏰆. B. Low-complexity Algorithm for Optimal Resource Allocation Given the value of xt in (P2), we denote the index set of users with xti = 1 as Mt1, and the complementary user set as Mt0. For simplicity of exposition, we drop the superscript t and express the optimal resource allocation problem that computes G 􏰅xt, ξt􏰆 as following (P4) : maximize 􏰀j∈M0 􏰕ajfj/φ − Yj(t)κfj3􏰖 + 􏰀i∈M1 {airi,O − Yi(t)ei,O} (28a) τ,f,eO,rO 17 ,建立了什么模型

最新推荐

recommend-type

新版Matlab中神经网络训练函数Newff的详细讲解-新版Matlab中神经网络训练函数Newff的使用方法.doc

ExamplesHere is a problem consisting of inputs P and targets T to be solved with a network.· P = [0 1 2 3 4 5 6 7 8 9 10];T = [0 1 2 3 4 3 2 1 2 3 4];Here a network is created with one hidden layer ...
recommend-type

华为OD机试D卷 - 用连续自然数之和来表达整数 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

Screenshot_2024-05-10-20-21-01-857_com.chaoxing.mobile.jpg

Screenshot_2024-05-10-20-21-01-857_com.chaoxing.mobile.jpg
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这