zoukankan      html  css  js  c++  java
  • 最短路径(Floyd-Warshall实现)

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <stdbool.h>
      5 
      6 #define MaxVertexNodeNumSize 1000
      7 #define MaxVertexNodeNameSize 100
      8 #define INFINITY 10000
      9 
     10 struct VertexBodyNode
     11 {
     12     char VertexName[MaxVertexNodeNameSize];
     13     int ArcWeight;
     14     int VertexIndex;
     15     struct VertexBodyNode *Next;
     16 };
     17 
     18 struct VertexHeadNode
     19 {
     20     char VertexName[MaxVertexNodeNameSize];
     21     int VertexWeight;
     22     struct VertexBodyNode *Next;
     23 };
     24 
     25 struct _Graph
     26 {
     27     struct VertexHeadNode VertexHeadNodeList[MaxVertexNodeNumSize];
     28     int ArcNum,VertexNum;
     29 };
     30 
     31 int VertexName2Index(struct _Graph *DirectedGraph,char *VName)
     32 {
     33     int i;
     34     for(i = 0; i < DirectedGraph -> VertexNum; i ++)
     35     {
     36         if(strcmp(DirectedGraph -> VertexHeadNodeList[i].VertexName,VName)==0)
     37         {
     38             return i;
     39         }
     40     }
     41     return -1;
     42 }
     43 
     44 void AddOneArc(struct _Graph *DirectedGraph,int ArcIndex_1,int ArcIndex_2,int AWeight)
     45 {
     46     struct VertexBodyNode *BNode = malloc(sizeof(struct VertexBodyNode));
     47 
     48     strcpy(BNode -> VertexName,DirectedGraph -> VertexHeadNodeList[ArcIndex_2].VertexName);
     49 
     50     BNode -> ArcWeight = AWeight;
     51     BNode -> VertexIndex = ArcIndex_2;
     52     BNode -> Next = NULL;
     53 
     54     struct VertexBodyNode *TmpPointer;
     55     TmpPointer = DirectedGraph -> VertexHeadNodeList[ArcIndex_1].Next;
     56     while(TmpPointer != NULL && TmpPointer -> Next != NULL)
     57     {
     58         TmpPointer = TmpPointer -> Next;
     59     }
     60     if(TmpPointer==NULL)
     61     {
     62         DirectedGraph -> VertexHeadNodeList[ArcIndex_1].Next = BNode;
     63     }
     64     else
     65     {
     66         TmpPointer -> Next = BNode;
     67     }
     68 }
     69 
     70 struct _Graph *UGCreat(int ArcSum,int VertexSum)
     71 {
     72     int i,j;
     73     struct _Graph *DirectedGraph = malloc(sizeof(struct _Graph));
     74     DirectedGraph -> ArcNum = ArcSum;
     75     DirectedGraph -> VertexNum = VertexSum;
     76 
     77     for(i = 0; i < VertexSum; i ++)
     78     {
     79         scanf("%s %d",DirectedGraph -> VertexHeadNodeList[i].VertexName,&DirectedGraph -> VertexHeadNodeList[i].VertexWeight);
     80     }
     81 
     82     for(i = 0; i < VertexSum; i ++)
     83     {
     84         DirectedGraph -> VertexHeadNodeList[i].Next = NULL;
     85     }
     86 
     87     for(i = 0; i < ArcSum; i ++)
     88     {
     89         char Arc_1[MaxVertexNodeNameSize];
     90         char Arc_2[MaxVertexNodeNameSize];
     91         int ArcIndex_1;
     92         int ArcIndex_2;
     93         int ArcWeight;
     94 
     95         scanf("%s %s %d",Arc_1,Arc_2,&ArcWeight);
     96 
     97         ArcIndex_1 = VertexName2Index(DirectedGraph,Arc_1);
     98         ArcIndex_2 = VertexName2Index(DirectedGraph,Arc_2);
     99 
    100         AddOneArc(DirectedGraph,ArcIndex_1,ArcIndex_2,ArcWeight);
    101     }
    102     return DirectedGraph;
    103 }
    104 
    105 void Travel(struct _Graph *DirectedGraph)
    106 {
    107     char StartingPoint[MaxVertexNodeNameSize];
    108     char OverPoint[MaxVertexNodeNameSize];
    109 
    110     printf("Input start and over
    ");
    111     scanf("%s %s",StartingPoint,OverPoint);
    112 
    113     int StartIndex = VertexName2Index(DirectedGraph,StartingPoint);
    114     int OverIndex = VertexName2Index(DirectedGraph,OverPoint);
    115 
    116     struct VertexBodyNode *TmpPointer;
    117     TmpPointer = DirectedGraph -> VertexHeadNodeList[StartIndex].Next;
    118     while(TmpPointer != NULL)
    119     {
    120         if(OverIndex==TmpPointer -> VertexIndex)
    121         {
    122             printf("Distance:%d GetVertexPointSum:%d",TmpPointer->ArcWeight
    123                    ,DirectedGraph -> VertexHeadNodeList[StartIndex].VertexWeight+DirectedGraph -> VertexHeadNodeList[OverIndex].VertexWeight);
    124             break;
    125         }
    126         else
    127         {
    128             TmpPointer = TmpPointer -> Next;
    129         }
    130     }
    131 }
    132 
    133 void FloydShortestPath(struct _Graph *DirectedGraph,char *Origin,char *Destination)
    134 {
    135     int OIndex = VertexName2Index(DirectedGraph,Origin);
    136     int DIndex = VertexName2Index(DirectedGraph,Destination);
    137     
    138     int i,j,k;
    139     struct VertexBodyNode *TmpPointer;
    140     int A[DirectedGraph->VertexNum][DirectedGraph->VertexNum];
    141     int Path[DirectedGraph->VertexNum][DirectedGraph->VertexNum];
    142     for(i = 0;i < DirectedGraph->VertexNum;i ++)
    143     {
    144         for(j = 0;j < DirectedGraph->VertexNum;j ++)
    145         {
    146             A[i][j] = INFINITY;
    147             if(i == j)
    148             {
    149                 A[i][j] = 0;
    150             }
    151             Path[i][j] = j;
    152         }
    153     }
    154     for(i = 0;i < DirectedGraph->VertexNum;i ++)
    155     {
    156         TmpPointer = DirectedGraph -> VertexHeadNodeList[i].Next;
    157         while(TmpPointer != NULL)
    158         {
    159             A[i][TmpPointer->VertexIndex] = TmpPointer -> ArcWeight;
    160             TmpPointer = TmpPointer -> Next;
    161         }
    162     }
    163     
    164     for(i = 0;i < DirectedGraph->VertexNum;i ++)
    165     {
    166         for(j = 0;j < DirectedGraph->VertexNum;j ++)
    167         {
    168             for(k = 0;k < DirectedGraph->VertexNum;k ++)
    169             {
    170                 if(A[j][k]>A[j][i]+A[i][k])
    171                 {
    172                     A[j][k] = A[j][i]+A[i][k];
    173                     Path[j][k] = Path[j][i];
    174                 }
    175             }
    176         } 
    177     } 
    178     
    179     int TmpIndex = Path[OIndex][DIndex];
    180     printf("%s -> ",DirectedGraph->VertexHeadNodeList[OIndex].VertexName);
    181     while(TmpIndex != DIndex)
    182     {
    183         printf("%s -> ",DirectedGraph->VertexHeadNodeList[TmpIndex].VertexName);
    184         TmpIndex = Path[TmpIndex][DIndex];
    185     }
    186     printf("%s    Distance:%d",DirectedGraph->VertexHeadNodeList[DIndex].VertexName,A[OIndex][DIndex]);
    187 }
    188 
    189 int main()
    190 {
    191     struct _Graph *G = UGCreat(8,5);
    192 
    193 //    Travel(G);
    194     char Origin[MaxVertexNodeNameSize];
    195     char Destination[MaxVertexNodeNameSize];
    196     printf("Input Origin && Destination:
    ");
    197     scanf("%s %s",Origin,Destination);
    198     FloydShortestPath(G,Origin,Destination);
    199     return 0;
    200 }
    201 
    202 /*
    203         beijing 18
    204         zhengzhou 10
    205         hefei 9
    206         nanjing 12
    207         guangzhou 14
    208         beijing zhengzhou 7
    209         beijing hefei 9
    210         beijing nanjing 8
    211         zhengzhou hefei 5
    212         hefei nanjing 3
    213         zhengzhou guangzhou 7
    214         hefei guangzhou 8
    215         nanjing guangzhou 6
    216 */
  • 相关阅读:
    【NOIP2016】换教室
    【NOIP模拟赛】总结
    【Codeforces Round 418】An impassioned circulation of affection DP
    DP测试总结
    【NOIP2012】疫情控制
    【HNOI2016】序列 莫队+单调栈+RMQ
    【Luogu P2709 小B的询问】莫队
    【HNOI2017】影魔
    【HNOI2017】大佬
    阿里云MaxCompute 2019-7月刊
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9427501.html
Copyright © 2011-2022 走看看