旅行家的预算

 

题目描述

一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的)。给定两个城市之间的距离D1、汽车油箱的容量C(以升为单位)、每升汽油能行驶的距离D2、出发点每升汽油价格P和沿途油站数N(N可以为零),油站i离出发点的距离Di、每升汽油价格Pi(i=1,2,…,N)。计算结果四舍五入至小数点后两位。如果无法到达目的地,则输出“No Solution”。

输入输出格式 Input/output

输入格式:
第一行,D1,C,D2,P,N。
接下来有N行。
第i+1行,两个数字,油站i离出发点的距离Di和每升汽油价格Pi。
输出格式:
所需最小费用,计算结果四舍五入至小数点后两位。如果无法到达目的地,则输出“No Solution”。

输入输出样例 Sample input/output

样例测试点#1
输入样例: 

275.6 11.9 27.4 2.8 2
102.0 2.9
220.0 2.2

输出样例:

26.95

 


1 #include<iostream> 2 #include<algorithm> 3 #include<iomanip> 4 using namespace std; 5 double d[10000], p[10000]; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 double d1, c, d2; 10 int n; 11 cin >> d1 >> c >> d2 >> p[0] >> n; 12 d[n + 1] = d1; 13 for (int i = 1; i <= n; i++) 14 { 15 cin >> d[i] >> p[i]; 16 } 17 int pos = 0; 18 double remain = 0, cost = 0; 19 do 20 { 21 bool found = false; 22 for (int i = pos + 1; i <= n + 1 && d[i] <= d[pos] + c*d2; i++) 23 { 24 if (p[i] < p[pos]) 25 { 26 if (d[pos] + remain*d2 >= d[i]) 27 { 28 remain -= (d[i] - d[pos]) / d2; 29 } 30 else 31 { 32 cost += ((d[i] - d[pos]) / d2 - remain)*p[pos]; 33 remain = 0; 34 } 35 pos = i; 36 found = true; 37 break; 38 } 39 } 40 if (!found) 41 { 42 cost += (c - remain)*p[pos]; 43 remain = c - (d[pos + 1] - d[pos]) / d2; 44 if (remain >= 0) pos++; 45 else 46 { 47 cout << "No Solution"; 48 return 0; 49 } 50 } 51 } while (pos <= n); 52 cout.setf(ios::fixed); 53 cout << setprecision(2) << cost; 54 }

 

转载于:https://www.cnblogs.com/Yinxinghan/p/4581562.html

### C++ 实现旅行家预算算法 #### 定义问题 旅行家预算是指给定一系列城市及其之间的交通费用,找到一条从起点到终点的路径,在满足总花费不超过指定预算的前提下,使得总的旅行距离最小。这个问题可以通过图论中的带权有向图来建模。 #### 数据结构设计 为了表示各个城市以及它们之间连接的成本,可以采用邻接矩阵或邻接表的形式存储图的信息。这里选用邻接列表以节省空间并便于处理稀疏图: ```cpp #include <vector> #include <queue> using namespace std; struct Edge { int to, cost; }; typedef vector<vector<Edge>> Graph; ``` #### Dijkstra变种算法求解 由于标准Dijkstra只考虑最短路径而不顾及额外约束条件(即预算),因此需要对其进行修改以便能够同时跟踪当前剩余的钱数。这可以通过引入三维数组`dist[i][j]`来完成,其中i代表节点编号而j则对应于还剩下多少钱的状态。当遍历过程中遇到新的更优状态时更新该表格,并继续扩展直到访问完所有可能的情况为止。 ```cpp const long long INF = 1e9; // 设置无穷大值 bool vis[105][10005]; // 记录是否已经到达过某一点某个金额下的最优情况 long long dist[105][10005]; // 初始化vis和dist为false/INF void init(int n, int m) { for (int i = 0; i <= n; ++i) for (int j = 0; j <= m; ++j){ vis[i][j] = false; dist[i][j] = INF; } } pair<int,int> dijkstra_modified(Graph &g, int start, int end, int budget) { priority_queue<pair<long long,pair<int,int>>, vector<pair<long long,pair<int,int>>>, greater<>> pq; dist[start][budget]=0; pq.push({0,{start,budget}}); while (!pq.empty()) { auto cur=pq.top(); pq.pop(); if(vis[cur.second.first][cur.second.second]) continue; vis[cur.second.first][cur.second.second]=true; if(cur.second.first==end)return {dist[end][cur.second.second],cur.second.second}; for(auto e:g[cur.second.first]){ if(e.cost<=cur.second.second&&!vis[e.to][cur.second.second-e.cost]){ if(dist[e.to][cur.second.second-e.cost]>dist[cur.second.first][cur.second.second]+1){ // 这里假设每条边长度都是1 dist[e.to][cur.second.second-e.cost]=dist[cur.second.first][cur.second.second]+1; pq.push({dist[e.to][cur.second.second-e.cost],{e.to,cur.second.second-e.cost}}); } } } } return {-1,-1}; // 如果找不到合适的方案,则返回-1作为标志位 } ``` 此代码片段展示了如何利用优先队列实现带有预算限制的广度优先搜索版本的Dijkstra算法[^3]。注意这里的cost字段用于记录实际开销而非传统意义上的权重;另外还需要特别关注边界条件如超支情形下应立即停止进一步探索分支。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值