Dijkstra算法

#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
#define INFINITE 1000
typedef struct graph
{
    int nodenum;
    int edgenum;
    int matrix[MAX_LEN][MAX_LEN];
}Graph;


typedef struct stack
{
    int  bottom;
    int  top;
    int  printout[MAX_LEN];
}mstack;


int in[MAX_LEN];
int Len[MAX_LEN];
int path[MAX_LEN];


void InitStack(mstack *s)
{
    s->bottom=0;
    s->top=0;
    memset(s->printout,0,sizeof(int)*MAX_LEN);
}


void push(mstack *s,int m)
{
    s->printout[s->top++]=m;
}


int pop(mstack *s)
{
   return s->printout[--s->top];
}


void InitGraph(Graph *g,int n)
{
    int i,j;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
          {
              if(i==j)g->matrix[i][j]=0;
              else g->matrix[i][j]=INFINITE;     
          }
          
     for(i=1;i<=n;i++)
     {
         in[i]=0;
         Len[i]=INFINITE;
         path[i]=0;
     }
}


int main()
{


    int n,m,i,A,B,templen,count,min,tempmin,si,temp;
    while(scanf("%d %d",&n,&m))
    {
        count=0;
        Graph mGraph;
        mGraph.edgenum=m;
        mGraph.nodenum=n;
        InitGraph(&mGraph,n);
        for(i=0;i<m;i++)
        {
            scanf("%d %d %d",&A,&B,&templen);
            mGraph.matrix[A][B]=templen;
        }
        


        in[1]=1;
        path[1]=1; //sava path
        Len[1]=0;
        
        for(i=2;i<=n;i++)
        {
            Len[i]=mGraph.matrix[1][i]; //Init the len
            if(Len[i]!=INFINITE)path[i]=1;
        }
        
        min=0;
        si=1;
       
        while(count<n-1)
        {
            tempmin=INFINITE;
            for(i=1;i<=n;i++)
            {
                if(in[i]==0&&Len[i]<tempmin) //find the smallest one
                {
                    tempmin=Len[i];
                    si=i;
                }
            }
            in[si]=1;
            
            for(i=1;i<=n;i++) //updata the length
            {
                if(in[i]==0&&(tempmin+mGraph.matrix[si][i])<Len[i])
                {
                    Len[i]=tempmin+mGraph.matrix[si][i];
                    path[i]=si;
                }
            }
            count++; 
        }
        
        mstack s;
        for(i=1;i<=n;i++)
        {
            temp=i;
            InitStack(&s);
            if(path[temp]==0)
            { 
                printf("no path\n");
                continue;
            }
            while(path[temp]!=1)
            {
                push(&s,path[temp]);
                temp=path[temp];
            }
            printf("1-->");
            while(s.bottom!=s.top)
            {
                printf("%d-->",pop(&s));
            }
            printf("%d  min length is %d\n",i,Len[i]);
            
        }
        
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值