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

发布于 2024-09-07 00:55:19 字数 2357 浏览 5 评论 0原文

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

如果有人可以改变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;
}

This Code is a code I built from the algorithm design manual book but I can't make it compile cause I've got little experience with pointers I think that's the main reason I think I can't compile it:

And if someone can change a little bit in the djikstra to make it through heap with the current configuration.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许仙没带伞 2024-09-14 00:55:19

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

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

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


您的代码出了什么问题?

您正在使用 >typedef-ed type 在编译器知道该类型之前。相反,您需要使用 Structure_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));

Change the definition of the struct, and it would compile.

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

While this will solve your problem, don't trust my answer below until someone better than me comments on it.


What was wrong in your code ?

You are using the typedef-ed type before the compiler knows about that type. Instead, you need to use the structure_tag to define the member pointer of type itself.

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*

EDIT:

Also, malloc returns *void**, which you need to cast to the type you are assigning it to.
So, inside function add_edges, make this correction (please read more about this in book, it is important to understand this):

                  p = (edgenode*)malloc(sizeof(edgenode));
π浅易 2024-09-14 00:55:19

typedef 结构体

{

int y;

整数权重;

struct EdgeNode *next;

} EdgeNode;

在这里,您使用的是 typedef 结构,但没有定义它,然后在定义 EdgeNode 之前在结构定义中使用 EdgeNode。


所以你应该将其更改为:

typedef struct _edgenode

{

int y;

整数权重;

struct _edgenode *next;

} edgenode;

typedef struct

{

int y;

int weight;

struct edgenode *next;

} edgenode;

Here you are using a typedef struct without defining this and then you are using edgenode in your struct defination before defining edgenode.


So you should change it to:

typedef struct _edgenode

{

int y;

int weight;

struct _edgenode *next;

} edgenode;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文