c++计算三维点云数据经过每个点最短路径源代码
时间: 2023-06-27 22:08:37 浏览: 109
求两个点之间最短路径C++源码.zip
5星 · 资源好评率100%
计算三维点云数据中每个点到起点的最短路径可以使用Dijkstra算法。以下是C++代码示例:
```c++
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
struct Point
{
double x, y, z;
};
struct Edge
{
int to;
double weight;
};
const int MAXN = 100000;
const double INF = 1e9;
vector<Edge> edges[MAXN];
double dist[MAXN];
bool vis[MAXN];
void addEdge(int from, int to, double weight)
{
edges[from].push_back({to, weight});
}
void dijkstra(int start)
{
priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> pq;
pq.push({0, start});
dist[start] = 0;
while (!pq.empty())
{
int u = pq.top().second;
pq.pop();
if (vis[u])
continue;
vis[u] = true;
for (Edge e : edges[u])
{
int v = e.to;
double w = e.weight;
if (dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}
}
int main()
{
int n, m, start;
cin >> n >> m >> start;
vector<Point> points(n);
for (int i = 0; i < n; i++)
{
cin >> points[i].x >> points[i].y >> points[i].z;
dist[i] = INF;
}
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
double weight = sqrt(pow(points[u].x - points[v].x, 2) + pow(points[u].y - points[v].y, 2) + pow(points[u].z - points[v].z, 2));
addEdge(u, v, weight);
}
dijkstra(start);
for (int i = 0; i < n; i++)
cout << "Shortest distance from point " << start << " to point " << i << " is " << dist[i] << endl;
return 0;
}
```
该程序先读入点云数据,然后根据输入的边构建图,每个边的权值为其两个端点之间的欧几里得距离。最后使用Dijkstra算法计算每个点到起点的最短路径,并输出结果。
阅读全文