vector<int>dist int i=dist.back() 什么意思
时间: 2024-03-16 12:44:51 浏览: 12
在 C++ 中,`std::vector<int> dist` 表示一个存储整型数据的向量(即动态数组),其中 `dist[i]` 表示从起点到节点 `i` 的距离。`dist.back()` 表示向量 `dist` 中的最后一个元素,即从起点到最后一个节点的距离。而 `int i = dist.back()` 则将这个距离的值赋给了整型变量 `i`。
这行代码的作用是将从起点到最后一个节点的距离赋给 `i`,方便后续使用。比如,如果你想计算从起点到最后一个节点的路径长度,可以使用这个值减去从起点到某个中间节点的距离,如下所示:
```
int j = 5; // 中间节点编号
int path_length = dist.back() - dist[j]; // 从起点到最后一个节点的距离减去从起点到中间节点的距离
```
这里假设节点 `5` 是路径上的某个中间节点,`path_length` 表示从起点到最后一个节点经过中间节点 `5` 的路径长度。
相关问题
请给一下代码加注释,越详细越好。AStar.h:#ifndef ASTAR_H #define ASTAR_H #include <vector> using namespace std; class AStar { public: AStar(int n); void add_edge(int u, int v, int w); void set_heuristic(vector<int>& h); void shortest_path(int s, int t); vector<int> get_dist(); vector<int> get_prev(); private: struct edge { int to, weight; edge(int t, int w) : to(t), weight(w) {} }; int n; vector<vector<edge>> graph; vector<vector<edge>> rev_graph; vector<int> dist; vector<int> prev; vector<int> heuristic; }; class Astar { }; #endif;AStar.cpp:#include "AStar.h" #include <vector> #include <queue> #include <limits> using namespace std; AStar::AStar(int n) : n(n), graph(n), rev_graph(n), dist(n, numeric_limits<int>::max()), prev(n, -1), heuristic(n, 0) {} void AStar::add_edge(int u, int v, int w) { graph[u].push_back(edge(v, w)); rev_graph[v].push_back(edge(u, w)); } void AStar::set_heuristic(vector<int>& h) { heuristic = h; } void AStar::shortest_path(int s, int t) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; dist[s] = 0; pq.push(make_pair(heuristic[s], s)); while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (u == t) return; for (auto& e : graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; prev[v] = u; pq.push(make_pair(dist[v] + heuristic[v], v)); } } for (auto& e : rev_graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; prev[v] = u; pq.push(make_pair(dist[v] + heuristic[v], v)); } } } } vector<int> AStar::get_dist() { return dist; } vector<int> AStar::get_prev() { return prev; }
注释已添加在代码中,以//开头。
AStar.h:
```cpp
#ifndef ASTAR_H
#define ASTAR_H
#include <vector>
using namespace std;
class AStar {
public:
//构造函数,传入节点数n
AStar(int n);
//添加一条从u到v,边权为w的边
void add_edge(int u, int v, int w);
//设置启发函数
void set_heuristic(vector<int>& h);
//求从s到t的最短路
void shortest_path(int s, int t);
//返回最短路长度
vector<int> get_dist();
//返回最短路的路径
vector<int> get_prev();
private:
//边,包括到达的点和边权
struct edge {
int to, weight;
edge(int t, int w) : to(t), weight(w) {}
};
int n; //节点数
vector<vector<edge>> graph; //正向图
vector<vector<edge>> rev_graph; //反向图
vector<int> dist; //最短路长度
vector<int> prev; //最短路的前驱
vector<int> heuristic; //启发函数
};
class Astar {
};
#endif;
```
AStar.cpp:
```cpp
#include "AStar.h"
#include <vector>
#include <queue>
#include <limits>
using namespace std;
AStar::AStar(int n) :
n(n),
graph(n),
rev_graph(n),
dist(n, numeric_limits<int>::max()), //初始化为无穷大
prev(n, -1), //初始化为-1
heuristic(n, 0) {} //初始化为0
void AStar::add_edge(int u, int v, int w) {
graph[u].push_back(edge(v, w)); //添加正向边
rev_graph[v].push_back(edge(u, w)); //添加反向边
}
void AStar::set_heuristic(vector<int>& h) {
heuristic = h;
}
void AStar::shortest_path(int s, int t) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
//小根堆,每个元素为(pair<估价函数值, 节点编号>)
dist[s] = 0; //起点到自身距离为0
pq.push(make_pair(heuristic[s], s)); //将起点加入堆
while (!pq.empty()) {
int u = pq.top().second; //取出堆顶节点
pq.pop();
if (u == t) return; //到达终点,直接返回
//遍历u的正向边
for (auto& e : graph[u]) {
int v = e.to;
int w = e.weight;
if (dist[v] > dist[u] + w) {
//通过u更新v的最短路
dist[v] = dist[u] + w;
prev[v] = u; //更新前驱
pq.push(make_pair(dist[v] + heuristic[v], v)); //将v加入堆
}
}
//遍历u的反向边
for (auto& e : rev_graph[u]) {
int v = e.to;
int w = e.weight;
if (dist[v] > dist[u] + w) {
//通过u更新v的最短路
dist[v] = dist[u] + w;
prev[v] = u; //更新前驱
pq.push(make_pair(dist[v] + heuristic[v], v)); //将v加入堆
}
}
}
}
vector<int> AStar::get_dist() {
return dist;
}
vector<int> AStar::get_prev() {
return prev;
}
```
在AStar.cpp中,主要实现了AStar类中的函数。其中,shortest_path函数是最重要的函数,该函数使用了A*算法来求解最短路。
该算法主要的思想是:将Dijkstra算法中的“距离”改为“估价函数值”,即f(n)=g(n)+h(n),g(n)表示从起点到当前节点的实际距离,h(n)表示从当前节点到终点的估计距离。在搜索过程中,每次从开放列表中选择f(n)值最小的节点进行扩展。在AStar.cpp中,使用了一个小根堆来维护开放列表,优化了搜索效率。
值得注意的是,A*算法并不能保证一定能够找到最短路径,但是在实际应用中,该算法的效率和准确性都得到了广泛的认可。
vector<int>dist的第一个点和最后一个点怎么表示
在 C++ 中,`std::vector<int> dist` 表示一个存储整型数据的向量(即动态数组)。如果要表示这个向量的第一个点和最后一个点,可以使用向量的下标来访问。
向量的第一个点是下标为 0 的元素,表示源点到自身的距离为 0,即 `dist[0] = 0`。向量的最后一个点是下标为 `dist.size() - 1` 的元素,表示源点到最后一个节点的距离,即 `dist[dist.size() - 1]`。
如果你想将向量的第一个点和最后一个点作为起点和终点构造一个向量,可以使用以下代码:
```
std::vector<int> vec = {dist.back() - dist.front(), 0}; // x 轴方向表示距离,y 轴方向为 0
```
这里使用了向量的 `back()` 和 `front()` 函数来访问最后一个点和第一个点的值。这样,`vec` 就表示了从源点到最后一个节点的向量,起点为源点,终点为最后一个节点。