c++ - 我无法编译此 dijkstra 代码。 (算法设计手册)

标签 c++ c graph dijkstra shortest-path

这段代码是我根据算法设计手册构建的代码,但我无法编译它,因为我对指针缺乏经验,我认为这是我无法编译它的主要原因:

如果有人可以在 djikstra 中进行一些更改,使其能够使用当前配置通过堆。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int MAXV=1000;
const int MAXINT=99999;

typedef struct{
    int y;
    int weight;
    struct edgenode *next;
}edgenode;
typedef struct{
    edgenode *edges[MAXV+1];
    int degree[MAXV+1];
    int nvertices;
    int nedges;
    bool directed;
}graph;

void add_edge(graph *g,int x,int y,int weight,bool directed);

void read_graph(graph *g,bool directed){
    int x,y,weight,m;
    g->nvertices=0;
    g->nedges=0;
    g->directed=directed;
    for(int i=1;i<MAXV;++i) g->degree[i]=0;
    for(int i=1;i<MAXV;++i) g->edges[i]=NULL;
    scanf("%d %d",&(g->nvertices),&m);
    for(int i=1;i<=m;++i){
        scanf("%d %d %d",&x,&y,&weight);
        add_edge(g,x,y,weight,directed);
    }
}

void add_edge(graph *g,int x,int y,int weight,bool directed){
    edgenode *p;
    p=malloc(sizeof(edgenode));
    p->weight=weight;
    p->y=y;
    p->next=g->edges[x];

    g->edges[x]=p;
    g->degree[x]++;
    if(directed==false) add_edge(g,y,x,weight,true);
    else g->nedges++;
}

int dijkstra(graph *g,int start,int end){
    edgenode *p;
    bool intree[MAXV+1];
    int distance[MAXV+1];
    for(int i=1;i<=g->nvertices;++i){
        intree[i]=false;
        distance[i]=MAXINT;
    }
    distance[start]=0;
    int v=start;
    while(intree[v]==false){
        intree[v]=true;
        p=g->edges[v];
        while(p!=NULL){
            int cand=p->y;
            int weight=p->weight;
            if(distance[cand] > distance[v]+weight) distance[cand]=distance[v]+weight;
            p=p->next;
        }
        v=1;
        int dist=MAXINT;
        for(int i=1;i<=g->nvertices;++i)
            if((intree[i]==false) && (dist > distance[i])){
                dist=distance[i];
                v=i;
            }
    }
    return distance[end];
}

int main(){
    graph g;
    read_graph(&g,false);
    int x=1,y,shortest;
    while(x!=0){
        scanf("%d %d",&x,&y);
        shortest=dijkstra(&g,x,y);
        printf("The shortest path from %d to %d is %d",x,y,shortest);
    }
    return 0;
}

最佳答案

更改结构体的定义,它就会编译。

struct edgenode_tag 
{
   int y;
   int weight;
   struct edgenode_tag *next;
};
typedef edgenode_tag edgenode; 

虽然这可以解决您的问题,但在比我更好的人发表评论之前,请不要相信我的回答。


您的代码出了什么问题?

在编译器知道该类型之前,您正在使用 typedef-ed type。相反,您需要使用structural_tag来定义类型本身的成员指针。

typedef struct 
{
  ...
  my_struct* pS;
  ...        
} my_struct;  // at this point compiler will know about *my_struct* type
              // Hence, you can not use that name until after this line.

              // To define the member pointer of type itself you need to 
              // to use the struct_tag, as I did in your example.
              // where, struct_tag is *edgenode_tag*

编辑:

此外,malloc 返回 *void**,您需要将其转换为要分配给它的类型。 因此,在函数 add_edges 内部,进行此更正(请在书中阅读更多相关内容,理解这一点很重要):

                  p = (edgenode*)malloc(sizeof(edgenode));

关于c++ - 我无法编译此 dijkstra 代码。 (算法设计手册),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3136625/

相关文章:

c++ - 英特尔 TBB : pool of graphs

c++ - 为什么(无限)递归在 clang(和 gcc/g++)中使用 和 w/o -O3 会给出不同的结果?

c++ - 在 C++ 中,是在堆栈或堆上的函数中创建 vector 还是映射?

c++ - GraphicsScene 中的轴承公式计算产生不稳定的结果

c - QuickSort 排序正整数,有时排序后第一个值是负整数

c - 预处理器

Bron-Kerbosch算法的C#实现

c++ - 类型转换指向 unique_ptr 的普通指针是一种不好的做法吗?

c - C的段错误

performance - 计算最小值 - 使用 maxflow 算法在有向加权图中进行切割