求助,学校学的dij板子,70分wa了三个点
查看原帖
求助,学校学的dij板子,70分wa了三个点
618374
Lelouch_J楼主2022/7/11 10:33

如代码

#include<stdio.h>
#include<stdlib.h>
#define MAX 10005
#define maxint 2147483647
#define ll long long
typedef struct Node{
	int adjvex;
    int quan;
	struct Node *nextarc;
}Node;
 
typedef struct Vnode{
	int data;
	Node *firstarc;
}Vnode,AdjList[MAX];
 
typedef struct ALGraph{
	AdjList vertices;
	int vexnum, arcnum;
}ALGraph;
 
int visited[MAX];
ll dist[MAX];

ALGraph create(int n,int m)
{
    ALGraph G;
    G.vexnum=n;
    G.arcnum=m;
    for(int i=1;i<=n;i++)
    {
        G.vertices[i].data=i;
        G.vertices[i].firstarc=NULL;
    }
    for(int i=1;i<=m;i++)
    {
        int a;
        Node *p=(Node*)malloc(sizeof(Node));
        scanf("%d %d %d",&a,&p->adjvex,&p->quan);
        p->nextarc=G.vertices[a].firstarc;
        G.vertices[a].firstarc=p;
    }
    return G;
}

void Dijkstra(ALGraph G,int n,int v)
{
    int i, j;
    for(i = 1; i <= n; i ++)
    {
        dist[i] = maxint;
    }
    Node *p=G.vertices[v].firstarc;
    while(p)
    {
        dist[p->adjvex]=p->quan;
        p=p->nextarc;
    }
    dist[v] = 0;
    visited[v] = 1;
    for(i = 2; i <= n; i++)
    {
        ll temp = maxint;
        int u = v;
        for(j = 1; j <= n; j ++)
        {
            if((visited[j]==0) && (dist[j]<=temp))
            {
                u = j;
                temp = dist[j];
            }
        }
        visited[u] = 1;
        Node *p=G.vertices[u].firstarc;
        while(p)
        {
            if((visited[p->adjvex]==0) && (p->quan<maxint))
            {
                ll newdist = dist[u]+p->quan;
                if(newdist < dist[p->adjvex])
                {
                    dist[p->adjvex] = newdist;
                }
            }
            p=p->nextarc;
        }
    }
}

int main()  
{  
    int n,m,s;
    scanf("%d %d %d",&n,&m,&s);
    ALGraph G= create(n,m);
    Dijkstra(G,n,s);
    for(int i=1;i<=n;i++)
    printf("%lld ",dist[i]);
} 
2022/7/11 10:33
加载中...