15:54:06 2019-09-03

学习

 

PTA 第18题 利用Floyd的算法 解决多源最短路问题

(os:读题没读懂 哎 语文理解力不行 )

  1 #define _CRT_SECURE_NO_WARNINGS  
  2 #include<stdio.h>
  3 #include<malloc.h>
  4 #define INIFITY 65635
  5 typedef struct ENode* Edge;
  6 struct ENode
  7 {
  8     int V1, V2;
  9     int Weight;
 10 };
 11 
 12 typedef struct Graph* MGraph;
 13 struct Graph
 14 {
 15     int Nv;
 16     int Ne;
 17     int G[200][200];
 18 };
 19 
 20 MGraph CreateGraph(int MaxVertex)
 21 {
 22     MGraph Graph = (MGraph)malloc(sizeof(struct Graph));
 23     Graph->Nv = MaxVertex;
 24     Graph->Ne = 0;
 25     for(int i=1;i<=Graph->Nv;i++)
 26         for (int j = 1; j <=Graph->Nv; j++)
 27         {
 28             if (i != j)
 29                 Graph->G[i][j] = INIFITY;
 30             else
 31                 Graph->G[i][j] = 0;
 32         }
 33     return Graph;
 34 }
 35 
 36 void Insert(MGraph Graph,Edge E)
 37 {
 38     Graph->G[E->V1][E->V2] = E->Weight;
 39     Graph->G[E->V2][E->V1] = E->Weight;
 40 }
 41 
 42 MGraph BuildGraph()
 43 {
 44     MGraph Graph;
 45     Edge E;
 46     int Nv;
 47     scanf("%d",  &Nv);
 48     Graph = CreateGraph(Nv);
 49     scanf("%d\n",&Graph->Ne);
 50     for (int i = 0; i < Graph->Ne; i++)
 51     {
 52         E = (Edge)malloc(sizeof(struct ENode));
 53         scanf("%d %d %d\n", &(E->V1), &(E->V2), &(E->Weight));
 54         Insert(Graph, E);
 55     }
 56     return Graph;
 57 }
 58 
 59 //多源最短路问题
 60 int Dist[200][200];
 61 int Path[200][200];
 62 void Floyd(MGraph Graph)
 63 {
 64     for(int i=1;i<=Graph->Nv;i++)
 65         for (int j = 1; j <= Graph->Nv; j++)
 66         {
 67             Dist[i][j] = Graph->G[i][j];
 68             Path[i][j] = -1;
 69         }
 70 
 71 
 72     for(int k=1;k<=Graph->Nv;k++)
 73         for(int i=1;i<=Graph->Nv;i++)
 74             for (int j = 1; j <= Graph->Nv; j++)
 75                 if (Dist[i][k] + Dist[k][j] < Dist[i][j])
 76                 {
 77                     Dist[i][j] = Dist[i][k] + Dist[k][j];
 78                     Path[i][j] = k;
 79                 }
 80 }
 81 void Judge(MGraph Graph)
 82 {
 83     int MaxChar[200] = { 0 };
 84     for (int i = 1; i <= Graph->Nv; i++)
 85         MaxChar[i] = Dist[i][1];
 86     for (int i = 1; i <=Graph->Nv; i++)
 87         for (int j = 1; j <= Graph->Nv; j++)
 88         {
 89             if (MaxChar[i] < Dist[i][j])
 90                 MaxChar[i] =Dist[i][j];
 91             if (Dist[i][j] == INIFITY)
 92             {
 93                 printf("%d", 0);
 94                 return;
 95             }
 96         }
 97 
 98     int Min= MaxChar[1];
 99     int Num=1;
100     for (int i = 1; i <= Graph->Nv; i++)
101     {
102         if (MaxChar[i]< Min)
103         {
104             Min = MaxChar[i];
105             Num = i;
106         }
107     }
108     printf("%d %d", Num, Min);
109 }
110 
111 int main()
112 {
113     MGraph Graph;
114     Graph = BuildGraph();
115     Floyd(Graph);
116     Judge(Graph);
117     return 0;
118 }
View Code

相关文章: