15:44:43 2019-09-04
勉強します
PTA第20题 Dijkstra算法的变形 其实是加了一个变量来辅助判别 那如果影响的判断的变量增加 就需要多加变量来 继续进行判断
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<malloc.h> 4 #define INIFITY 65635 5 int Departure; 6 int Destination; 7 typedef struct TypeInt Element; 8 struct TypeInt 9 { 10 int Length; 11 int Price; 12 }; 13 typedef struct ENode* Edge; 14 struct ENode 15 { 16 int V1, V2; 17 int Length; 18 int Price; 19 }; 20 21 typedef struct Graph* MGraph; 22 struct Graph 23 { 24 int Nv; 25 int Ne; 26 Element G[500][500]; 27 }; 28 29 MGraph CreateGraph(int MaxVertex) 30 { 31 MGraph Graph = (MGraph)malloc(sizeof(struct Graph)); 32 Graph->Nv = MaxVertex; 33 Graph->Ne = 0; 34 for (int i = 0; i < Graph->Nv; i++) 35 for (int j = 0; j < Graph->Nv; j++) 36 { 37 Graph->G[i][j].Length= INIFITY; 38 Graph->G[i][j].Price = INIFITY; 39 } 40 return Graph; 41 } 42 43 void Insert(MGraph Graph, Edge E) 44 { 45 Graph->G[E->V1][E->V2].Length= E->Length; 46 Graph->G[E->V2][E->V1].Length= E->Length; 47 Graph->G[E->V1][E->V2].Price = E->Price; 48 Graph->G[E->V2][E->V1].Price = E->Price; 49 } 50 51 MGraph BuildGraph() 52 { 53 MGraph Graph; 54 Edge E; 55 int Nv; 56 scanf("%d", &Nv); 57 Graph = CreateGraph(Nv); 58 scanf("%d %d %d", &Graph->Ne,&Departure,&Destination); 59 for (int i = 0; i < Graph->Ne; i++) 60 { 61 E = (Edge)malloc(sizeof(struct ENode)); 62 scanf("%d %d %d %d\n", &(E->V1), &(E->V2), &(E->Length),&(E->Price)); 63 Insert(Graph, E); 64 } 65 return Graph; 66 } 67 68 int IsEdge(MGraph Graph, int V, int W) 69 { 70 return (Graph->G[V][W].Length < INIFITY) ? 1 : 0; 71 } 72 int Dist[500]; //从源点到某点的路径长度 73 int Price[500]; //从源点到某点的价格 74 int Path[500]; //记录路径 75 int Collected[500]; //记录是否收录 76 int FindMinDist(MGraph Graph) 77 { 78 int MinDist = INIFITY; 79 int V; 80 for (int i = 0; i < Graph->Nv; i++) 81 { 82 if (!Collected[i]&&Dist[i] < MinDist) 83 { 84 MinDist = Dist[i]; 85 V = i; 86 } 87 } 88 if (MinDist < INIFITY) 89 return V; 90 else 91 return 0; 92 } 93 void Dijkstra(MGraph Graph,int S) 94 { 95 //先初始化源点连接的点 96 for (int i = 0; i < Graph->Nv; i++) 97 { 98 Dist[i] = Graph->G[S][i].Length; 99 Price[i] = Graph->G[S][i].Price; 100 if (IsEdge(Graph,S,i)) 101 Path[i] = S; 102 else 103 Path[i] = -1; 104 } 105 106 Collected[S] = 1; 107 Dist[S] = 0; 108 Price[S] = 0; 109 110 while (1) 111 { 112 int V = FindMinDist(Graph); 113 if (!V) 114 break; 115 Collected[V] = 1; 116 for (int i = 0; i < Graph->Nv; i++) 117 { 118 if (!Collected[i] && IsEdge(Graph, V, i)) 119 { 120 if (Dist[V] + Graph->G[V][i].Length < Dist[i]) 121 { 122 Dist[i] = Dist[V] + Graph->G[V][i].Length; 123 Path[i] = V; 124 Price[i] = Price[V] + Graph->G[V][i].Price; 125 } 126 else if (Dist[V] + Graph->G[V][i].Length == Dist[i]) 127 { 128 if (Price[V] + Graph->G[V][i].Price < Price[i]) 129 { 130 Price[i] = Price[V] + Graph->G[V][i].Price; 131 Path[i] = V; 132 } 133 } 134 } 135 136 } 137 } 138 } 139 int main() 140 { 141 MGraph Graph = BuildGraph(); 142 Dijkstra(Graph,Departure); 143 printf("%d %d", Dist[Destination], Price[Destination]); 144 return 0; 145 }