国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Dijkstra算法的C語言程序

2019-11-14 09:46:12
字體:
供稿:網(wǎng)友

Dijikstra算法是從一個頂點(diǎn)到其余各頂點(diǎn)的最短路徑算法,解決的是有向圖中最短路徑問題。該算法是由荷蘭計算機(jī)科學(xué)家迪杰斯特拉于1959年提出的。

程序來源:Dijkstra's Algorithm。

百度百科:Dijkstra算法。

維基百科:Dijkstra's Algorithm。

C語言程序(去除了原文中非標(biāo)準(zhǔn)的C語言代碼):

#include<stdio.h>#define INFINITY 9999#define MAX 10void dijikstra(int G[MAX][MAX], int n, int startnode);int main(){    int G[MAX][MAX], i, j, n, u;    PRintf("/nEnter the no. of vertices:: ");    scanf("%d", &n);    printf("/nEnter the adjacency matrix::/n");    for(i=0;i < n;i++)        for(j=0;j < n;j++)            scanf("%d", &G[i][j]);    printf("/nEnter the starting node:: ");    scanf("%d", &u);    dijikstra(G,n,u);}void dijikstra(int G[MAX][MAX], int n, int startnode){    int cost[MAX][MAX], distance[MAX], pred[MAX];    int visited[MAX], count, mindistance, nextnode, i,j;    for(i=0;i < n;i++)        for(j=0;j < n;j++)            if(G[i][j]==0)                cost[i][j]=INFINITY;            else                cost[i][j]=G[i][j];    for(i=0;i< n;i++) {        distance[i]=cost[startnode][i];        pred[i]=startnode;        visited[i]=0;    }    distance[startnode]=0;    visited[startnode]=1;    count=1;    while(count < n-1) {        mindistance=INFINITY;        for(i=0;i < n;i++)            if(distance[i] < mindistance&&!visited[i]) {                mindistance=distance[i];                nextnode=i;            }        visited[nextnode]=1;        for(i=0;i < n;i++)            if(!visited[i])                if(mindistance+cost[nextnode][i] < distance[i]) {                    distance[i]=mindistance+cost[nextnode][i];                    pred[i]=nextnode;                }            count++;    }    for(i=0;i < n;i++)        if(i!=startnode) {            printf("/nDistance of %d = %d", i, distance[i]);            printf("/nPath = %d", i);            j=i;            do {                j=pred[j];                printf(" <-%d", j);            }            while(j!=startnode);        }    printf("/n");}程序運(yùn)行結(jié)果:

Enter the no. of vertices:: 4Enter the adjacency matrix::0 1 1 11 0 1 01 1 0 11 0 1 0Enter the starting node:: 1Distance of 0 = 1Path = 0 <-1Distance of 2 = 1Path = 2 <-1Distance of 3 = 2Path = 3 <-0 <-1


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 格尔木市| 宜良县| 崇礼县| 板桥市| 清镇市| 鲁甸县| 色达县| 正阳县| 舟山市| 景德镇市| 恭城| 临海市| 洪泽县| 博爱县| 竹山县| 舟山市| 宣武区| 大埔区| 武宣县| 华宁县| 当阳市| 青田县| 尖扎县| 吉安市| 彰武县| 永寿县| 奉贤区| 尼勒克县| 沙雅县| 临江市| 盐池县| 姜堰市| 康平县| 安徽省| 余干县| 故城县| 崇左市| 扬中市| 江西省| 信宜市| 安多县|