栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

西工大 数据结构 noj22.Dijkstra算法(严7.42)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

西工大 数据结构 noj22.Dijkstra算法(严7.42)

考试周,不多说了

要注意的就是最后输出的时候要按路径长度排序再输出

直接上代码

#include
#include
#define MAX 108
#define maxint 1000 

typedef struct Node{
	int adjvex;
    int quan;
	struct Node *nextarc;
}Node;
 
typedef struct Vnode{
	int data;
	struct Node *firstarc;
}Vnode,AdjList[MAX];
 
typedef struct ALGraph{
	AdjList vertices;
	int vexnum, arcnum;
}ALGraph;
 
int visited[MAX];
int 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 m)
{
    int i, j;
    for(i = 1; i <= n; i ++)
    {
        dist[i] = maxint;
    }
    Node *p=G.vertices[1].firstarc;
    while(p)
    {
        dist[p->adjvex]=p->quan;
        p=p->nextarc;
    }
    dist[1] = 0;
    visited[1] = 1;
    for(i = 2; i <= n; i++)
    {
        int temp = maxint;
        int u = 1;
        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->quanquan;
                if(newdist < dist[p->adjvex])
                {
                    dist[p->adjvex] = newdist;
                }
            }
            p=p->nextarc;
        }
    }
}

void print(ALGraph G,int n)
{
    int flag[MAX]={0};
    for(int i = 2; i <= n; i++)
    {
        int temp=1001,u;
        for(int j=2;j<=n;j++)
        {
            if(flag[j]==0 && dist[j] 

感谢观看

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975160.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号