Algorithm 去吧,迪克斯特拉:打印路径,而不仅仅是计算最短距离
去吧,迪克斯特拉:打印路径,而不仅仅是计算最短距离 我能用Dijkstra算法找到最短的距离,也许不能。 代码可以在这里找到 但是如果我不能打印出路径,那就没用了。 由于指针太多,我不知道如何打印出权重最少的最终路径 简言之,如何在给定的代码上不仅找到最短距离,而且打印出最短路径 链接如下: 下面是代码片段:Algorithm 去吧,迪克斯特拉:打印路径,而不仅仅是计算最短距离,algorithm,graph,go,dijkstra,shortest-path,Algorithm,Graph,Go,Dijkstra,Shortest Path,去吧,迪克斯特拉:打印路径,而不仅仅是计算最短距离 我能用Dijkstra算法找到最短的距离,也许不能。 代码可以在这里找到 但是如果我不能打印出路径,那就没用了。 由于指针太多,我不知道如何打印出权重最少的最终路径 简言之,如何在给定的代码上不仅找到最短距离,而且打印出最短路径 链接如下: 下面是代码片段: const MAXWEIGHT = 1000000 type MinDistanceFromSource map[*Vertex]int func (G *Graph) Dijks
const MAXWEIGHT = 1000000
type MinDistanceFromSource map[*Vertex]int
func (G *Graph) Dijks(StartSource, TargetSource *Vertex) MinDistanceFromSource {
D := make(MinDistanceFromSource)
for _, vertex := range G.VertexArray {
D[vertex] = MAXWEIGHT
}
D[StartSource] = 0
for edge := range StartSource.GetAdEdg() {
D[edge.Destination] = edge.Weight
}
CalculateD(StartSource, TargetSource, D)
return D
}
func CalculateD(StartSource, TargetSource *Vertex, D MinDistanceFromSource) {
for edge := range StartSource.GetAdEdg() {
if D[edge.Destination] > D[edge.Source]+edge.Weight {
D[edge.Destination] = D[edge.Source] + edge.Weight
} else if D[edge.Destination] < D[edge.Source]+edge.Weight {
continue
}
CalculateD(edge.Destination, TargetSource, D)
}
}
在此处调整新路径距离时
if D[edge.Destination] > D[edge.Source]+edge.Weight {
D[edge.Destination] = D[edge.Source] + edge.Weight
设置一些数组元素(例如,“parent”的p
)以指向您从Source
到达的目的地
P[edge.Destination] = edge.Source
算法结束后,在此数组中,每个顶点都将在从起始顶点开始的路径上具有其前身
好的,不是数组和索引
将新字段Prev
添加到顶点:
type Vertex struct {
Id string
Visited bool
AdjEdge []*Edge
Prev *Vertex
}
调整距离时:
if D[edge.Destination] > D[edge.Source]+edge.Weight {
D[edge.Destination] = D[edge.Source] + edge.Weight
edge.Destination.Prev = edge.Source
当显示结果时:
for vertex1, distance1 := range distmap1 {
fmt.Println(vertex1.Id, "=", distance1)
if vertex1.Prev != nil {
fmt.Println (vertex1.Id, " -> ", vertex1.Prev.Id)
}
}
使用Dijkstra的Graph算法打印最短路径(这里它是针对无向图实现的。下面的代码打印从源节点到图中所有其他节点的最短距离
它还打印从源节点到用户请求的节点的最短路径。
假设需要在图中找到从A到B的最短路径,然后输入A作为源节点,B作为目标节点
代码
#include<bits/stdc++.h>
using namespace std;
#define INF (unsigned)!((int)0)
const int MAX=2e4;
vector<pair<int,int>> graph[MAX];
bool visit[MAX];
int dist[MAX];
multiset<pair<int,int>> s;
int parent[MAX]; // used to print the path
int main(){
memset(visit,false,sizeof(visit));
memset(dist,INF,sizeof(dist));
memset(parent,-1,sizeof(parent));
int nodes,edges; cin>>nodes>>edges;
for(auto i=0;i<edges;++i){
int a,b,w;
cin>>a>>b>>w;
graph[a].push_back(make_pair(b,w));
graph[b].push_back(make_pair(a,w)); //Comment it to make the Directed Graph
}
int source_node; cin>>source_node;
dist[source_node]=0;
s.insert(make_pair(0,source_node));
while(!s.empty()){
pair<int,int> elem=*s.begin();
s.erase(s.begin());
int node=elem.second;
if(visit[node])continue;
visit[node]=true;
for(auto i=0;i<graph[node].size();++i){
int dest=graph[node][i].first;
int w=graph[node][i].second;
if(dist[node]+w<dist[dest]){
dist[dest]=dist[node]+w;
parent[dest]=node;
s.insert(make_pair(dist[dest],dest));
}
}
}
cout<<"NODE"<<" "<<"DISTANCE"<<endl;
for(auto i=1;i<=nodes;++i){
cout<<i<<" "<<dist[i]<<endl;
}
/*----PRINT SHORTEST PATH FROM THE SOURCE NODE TO THE NODE REQUESTED-------*/
int node_for_path; cin>>node_for_path;
int dest_node=node_for_path;
stack<int> path;
while(parent[node_for_path]!=source_node){
path.push(node_for_path);
node_for_path=parent[node_for_path];
}
path.push(node_for_path);
path.push(source_node);
cout<<"Shortest Path from "<<source_node<<"to "<<dest_node<<":"<<endl;
while(!path.empty()){
if(path.size()==1) cout<<path.top();
else cout<<path.top()<<"->";
path.pop();
}
return 0;
}
/*TEST CASE*/
9 14 //---NODES,EDGES---
1 2 4 //---START,END,WEIGHT---FOR THE NO OF EDGES
2 3 8
3 4 7
4 5 9
5 6 10
6 7 2
7 8 1
8 1 8
2 8 11
8 9 7
9 7 6
9 3 2
6 3 4
4 6 14
1 //---SOURCE_NODE
5 //-----NODE TO WHICH PATH IS REQUIRED
---END---*/
#包括
使用名称空间std;
#定义INF(无符号)!((int)0)
常数int MAX=2e4;
向量图[MAX];
布尔访问[MAX];
int dist[MAX];
多集s;
int parent[MAX];//用于打印路径
int main(){
memset(visit,false,sizeof(visit));
memset(dist、INF、sizeof(dist));
memset(父级,-1,sizeof(父级));
int节点,边;cin>>节点>>边;
对于(自动i=0;i>a>>b>>w;
图[a]。推回(形成一对(b,w));
图[b]。推回(生成有向图对(a,w));//注释它以生成有向图
}
int源节点;cin>>源节点;
dist[源节点]=0;
s、 插入(生成_对(0,源_节点));
而(!s.empty()){
pair elem=*s.begin();
s、 删除(s.begin());
int node=elem.second;
如果(访问[节点])继续;
访问[节点]=真;
对于(auto i=0;你能详细解释一下吗?你说的设置数组元素是什么意思?我应该用什么来初始化数组?P是一个数组,那么索引怎么可以是顶点类型?谢谢!@检查答案,我更正了一点。你用一些无效的顶点索引初始化数组。这就是你说的吗?这给了我非integer slice index edge.DestinationI也做了类似的事情,但仍然无法将这些结果结合起来
#include<bits/stdc++.h>
using namespace std;
#define INF (unsigned)!((int)0)
const int MAX=2e4;
vector<pair<int,int>> graph[MAX];
bool visit[MAX];
int dist[MAX];
multiset<pair<int,int>> s;
int parent[MAX]; // used to print the path
int main(){
memset(visit,false,sizeof(visit));
memset(dist,INF,sizeof(dist));
memset(parent,-1,sizeof(parent));
int nodes,edges; cin>>nodes>>edges;
for(auto i=0;i<edges;++i){
int a,b,w;
cin>>a>>b>>w;
graph[a].push_back(make_pair(b,w));
graph[b].push_back(make_pair(a,w)); //Comment it to make the Directed Graph
}
int source_node; cin>>source_node;
dist[source_node]=0;
s.insert(make_pair(0,source_node));
while(!s.empty()){
pair<int,int> elem=*s.begin();
s.erase(s.begin());
int node=elem.second;
if(visit[node])continue;
visit[node]=true;
for(auto i=0;i<graph[node].size();++i){
int dest=graph[node][i].first;
int w=graph[node][i].second;
if(dist[node]+w<dist[dest]){
dist[dest]=dist[node]+w;
parent[dest]=node;
s.insert(make_pair(dist[dest],dest));
}
}
}
cout<<"NODE"<<" "<<"DISTANCE"<<endl;
for(auto i=1;i<=nodes;++i){
cout<<i<<" "<<dist[i]<<endl;
}
/*----PRINT SHORTEST PATH FROM THE SOURCE NODE TO THE NODE REQUESTED-------*/
int node_for_path; cin>>node_for_path;
int dest_node=node_for_path;
stack<int> path;
while(parent[node_for_path]!=source_node){
path.push(node_for_path);
node_for_path=parent[node_for_path];
}
path.push(node_for_path);
path.push(source_node);
cout<<"Shortest Path from "<<source_node<<"to "<<dest_node<<":"<<endl;
while(!path.empty()){
if(path.size()==1) cout<<path.top();
else cout<<path.top()<<"->";
path.pop();
}
return 0;
}
/*TEST CASE*/
9 14 //---NODES,EDGES---
1 2 4 //---START,END,WEIGHT---FOR THE NO OF EDGES
2 3 8
3 4 7
4 5 9
5 6 10
6 7 2
7 8 1
8 1 8
2 8 11
8 9 7
9 7 6
9 3 2
6 3 4
4 6 14
1 //---SOURCE_NODE
5 //-----NODE TO WHICH PATH IS REQUIRED
---END---*/