最短路 迪傑斯特拉.cpp

來源:互聯網
上載者:User

標籤:des   style   blog   color   2014   演算法   

<span style="color:#3333ff;">#include<stdio.h>#include<stdlib.h>#define INITITY 999//最大值#define VERTEX 20//最多頂點個數#define FALSE 0#define TURE 1#define size 30#define OVERFLOW -1typedef struct ArcCell{   int adj;//權實值型別}ArcCell,AdjMatrix[VERTEX][VERTEX];typedef struct{  char vexs[20];//頂點向量  AdjMatrix arcs;//鄰接矩陣  int vexnum,arcnum;//圖的當前頂點數和弧數}MGraph;typedef struct{  char *top,*base;//棧頂指標,棧尾指標}Stack;int P[20][20];void InitStack(Stack *S)//初始化棧{   S->base=(char *)malloc(VERTEX*sizeof(char));   S->top=S->base;}void DestoyStock(Stack *S)//銷毀棧{  S->top=S->base;}void PushStack(Stack *S,char a)//元素壓棧{ if(S->top-S->base>size)    exit(OVERFLOW);  else *S->top++=a;}void PrintfStack(Stack *S)//輸出棧{ if(S->top==S->base)    exit(OVERFLOW);    printf("%c",*--S->top);  while(S->top!=S->base)    printf("->%c",*--S->top);}int Located(MGraph *G,char v1)//尋找頂點v1的位置{ int i;   for(i=1;i<=G->vexnum;i++) if(G->vexs[i]==v1) return i;}void CreateUDN(MGraph *G){   int i,j,k,w;    char v1,v2;   printf("請輸入圖的頂點數:");     scanf("%d",&G->vexnum);getchar();  for(i=1;i<=G->vexnum;i++)        G->vexs[i]=0;//頂點向量置零   for(i=1;i<=G->vexnum;i++){      printf("請輸入第%d個頂點:",i),      scanf("%c",&G->vexs[i]),getchar();//構造頂點向量      }   for(i=1;i<=G->vexnum;i++)      for(j=1;j<=G->vexnum;j++)    G->arcs[i][j].adj=999;//鄰接矩陣初始化   printf("請輸入圖的弧數:");      scanf("%d",&G->arcnum);getchar();   for(k=1;k<=G->arcnum;k++)     {       printf("請輸入第%d條弧向量<v1,v2,weight>:",k);        scanf("%c,%c,%d",&v1,&v2,&w);getchar();        i=Located(G,v1); j=Located(G,v2);//尋找位置        G->arcs[i][j].adj=w;//更新權值最小        G->arcs[j][i]=G->arcs[i][j];   }}void ShortestPath_DIJ(MGraph *G,int u,int *D)//求最短路徑,迪傑斯特拉演算法{int final[VERTEX],v,w,i,min,k=1;   for(v=1;v<=G->vexnum;v++)     {       final[v]=FALSE;D[v]=G->arcs[u][v].adj;       for(w=1;w<=G->vexnum;w++)         P[v][w]=FALSE;//設空路徑     }        D[u]=0,final[u]=FALSE;//初始化,u屬於S      for(i=1;i<=G->vexnum;i++){//開始主迴圈,每一次求得u到其他頂點的最短路徑        min=INITITY;//當前所知的離u頂點的最短距離        for(w=0;w<=G->vexnum;w++)           if(!final[w])//w頂點在V-S中              if(D[w]<min){//w頂點離u更近                 v=w;min=D[w];                 }        final[v]=TURE;//離u更近的v加入中        for(w=0;w<=G->vexnum;w++)//更新當前最短路徑及距離           if(!final[w]&&(min+G->arcs[v][w].adj<D[w])){//修改D[w]和P[w]           D[w]=min+G->arcs[v][w].adj;           P[u][w]=v;P[w][u]=v;           }         }}int main(){  int j,u,D[20],a;   MGraph G;   Stack S;InitStack(&S);//初始化棧   CreateUDN(&G);   printf("\n****************************最短路徑************************************\n");   for(u=1;u<G.vexnum;u++){   printf("以%c為起點:\n",G.vexs[u]);    ShortestPath_DIJ(&G,u,D);//求最短路徑    for(j=u+1;j<=G.vexnum;j++){     printf("    %c->%c的最短路徑為:",G.vexs[u],G.vexs[j]);       if(P[u][j]==0)//如果P[u][j]=0,則兩點之間的最短路徑是直達的,只輸出兩個頂點即可          printf("%c->%c",G.vexs[u],G.vexs[j]),          printf("    權值:%d ",D[j]),printf("\n");       else{//不是直達,則將其所經節點依次存入棧中,最後輸出         PushStack(&S,G.vexs[j]);         a=P[u][j];//a=0,則u與j之間是直達的,否則之間還經過其他的節點         while(a!=0)//中間所經節點依次壓入棧中         {           PushStack(&S,G.vexs[a]);           a=P[u][a];         }           PushStack(&S,G.vexs[u]);//將起始節點壓入棧中           PrintfStack(&S);           printf("    權值:%d ",D[j]);printf("\n");           DestoyStock(&S);}       }      }  return 0;}</span>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.