本博客的代碼的思想和圖片參考:好大學慕課浙江大學陳越老師、何欽銘老師的《數據結構》
多源最短路徑算法
1.使用Dijkstra算法對每個頂點運行一次運算,可以得到每個頂點到最圖所有頂點的最小值,時間復雜度為:T = O( |V| 3 + |E||V|)。該算法對稀疏圖比較好
2.使用Floyd算法,時間復雜度為:T = O( |V| 3 ),該算法對稠密圖比較好
下面介紹Floyd算法
D k [i][j] = 路徑{ i --> { l --> k } --> j }的最小長度
D 0 , D 1 , ..., D |V|-1 [i][j]即給出了i到j的真正最短距離
最初的D -1 是什么?
當D k-1 已經完成,遞推到D k 時:
或者k 屬於最短路徑{ i --> { l --> k } --> j },則D k = D k-1
或者k 屬於最短路徑{ i --> { l --> k } --> j },則該路徑必定由兩
段最短路徑組成: D k [i][j]=D k-1 [i][k]+D k-1 [k][j]
void Floyd()
{ for ( i = 0; i < N; i++ )
for( j = 0; j < N; j++ ) {
D[i][j] = G[i][j];
path[i][j] = -1;
}
for( k = 0; k < N; k++ )
for( i = 0; i < N; i++ )
for( j = 0; j < N; j++ )
if( D[i][k] + D[k][j] < D[i][j] ) {
D[i][j] = D[i][k] + D[k][j];
path[i][j] = k;
}
}

1 /* 2 * floyd.c 3 * 4 * Created on: 2017年5月14日 5 * Author: ygh 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 #define MAX_VERTEX_NUM 100 /*define the max number of the vertex*/ 11 #define INFINITY 65535 /*define double byte no negitive integer max number is 65535*/ 12 #define ERROR -1 13 14 typedef int vertex; /*define the data type of the vertex*/ 15 typedef int weightType; /*define the data type of the weight*/ 16 typedef char dataType; /*define the data type of the vertex value*/ 17 18 /*define the data structure of the Edge*/ 19 typedef struct eNode *ptrToENode; 20 typedef struct eNode { 21 vertex v1, v2; /*two vertex between the edge <v1,v2>*/ 22 weightType weight; /*the value of the edge's weight */ 23 }; 24 typedef ptrToENode edge; 25 26 /*define the data structure of the graph*/ 27 typedef struct gNode *ptrToGNode; 28 typedef struct gNode { 29 int vertex_number; /*the number of the vertex*/ 30 int edge_nunber; /*the number of the edge*/ 31 weightType g[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; /*define the adjacent matrix weight of graph*/ 32 dataType data[MAX_VERTEX_NUM]; /*define the dataType array to store the value of vertex*/ 33 }; 34 typedef ptrToGNode adjacentMatrixGraph; /*a graph show by adjacent matrix*/ 35 36 /* 37 create a graph given the vertex number. 38 @param vertexNum The verter number of the graph 39 @return a graph with vertex but no any egdgs 40 */ 41 adjacentMatrixGraph createGraph(int vertexNum) { 42 vertex v, w; 43 adjacentMatrixGraph graph; 44 graph = (adjacentMatrixGraph) malloc(sizeof(struct gNode)); 45 graph->vertex_number = vertexNum; 46 graph->edge_nunber = 0; 47 /*initialize the adjacent matrix*/ 48 for (v = 0; v < graph->vertex_number; v++) { 49 for (w = 0; w < graph->vertex_number; w++) { 50 graph->g[v][w] = INFINITY; 51 } 52 } 53 54 return graph; 55 } 56 57 /* 58 insert a edge to graph.We will distinct oriented graph and undirected graph 59 @param graph The graph you want to insert edge 60 @param e The edge you want to insert the graph 61 @param isOriented Whether the graph is oriented graph.If the graph is oriented 62 we will set adjacent matrix [n][m]=[m][n]=edge's weight,else we only set 63 the adjacent matrix [n][m]=edge's weight 64 */ 65 void inserEdge(adjacentMatrixGraph graph, edge e, int isOriented) { 66 graph->g[e->v1][e->v2] = e->weight; 67 if (!isOriented) { 68 graph->g[e->v2][e->v1] = e->weight; 69 } 70 } 71 72 /* 73 construct a graph according user's input 74 75 @return a graph has been filled good 76 */ 77 adjacentMatrixGraph buildGraph(int isOrdered) { 78 adjacentMatrixGraph graph; 79 edge e; 80 vertex i; 81 int vertex_num; 82 scanf("%d", &vertex_num); 83 graph = createGraph(vertex_num); 84 scanf("%d", &(graph->edge_nunber)); 85 if (graph->edge_nunber) { 86 e = (edge) malloc(sizeof(struct eNode)); 87 for (i = 0; i < graph->edge_nunber; i++) { 88 scanf("%d %d %d", &e->v1, &e->v2, &e->weight); 89 e->v1--; 90 e->v2--; 91 inserEdge(graph, e, isOrdered); 92 } 93 } 94 return graph; 95 96 } 97 98 /* 99 * Floyd algorithms: 100 1.D k [i][j] = 路徑{ i --> { l --> k } --> j }的最小長度 101 2.D 0 , D 1 , ..., D |V|-1 [i][j]即給出了i到j的真正最短距離 102 3.最初的D -1 是什么? 103 4.當D k-1 已經完成,遞推到D k 時: 104 或者k 屬於最短路徑{ i --> { l --> k } --> j },則D k = D k-1 105 或者k 屬於最短路徑{ i --> { l --> k } --> j },則該路徑必定由兩 106 段最短路徑組成: D k [i][j]=D k1 [i][k]+D k1 [k][j] 107 *@param graph A graph store by adjacent matrix 108 *@param d A two-dimensional array to store the distance value 109 *@param path A two-dimensional array to store the path 110 */ 111 int floyd(adjacentMatrixGraph graph, weightType d[][MAX_VERTEX_NUM], 112 vertex path[][MAX_VERTEX_NUM]) { 113 vertex i, j, k; 114 /* 115 * Initialize array 116 */ 117 for (i = 0; i < graph->vertex_number; i++) { 118 for (j = 0; j < graph->vertex_number; j++) { 119 d[i][j] = graph->g[i][j]; 120 path[i][j] = -1; 121 } 122 } 123 for (k = 0; k < graph->vertex_number; k++) { 124 for (i = 0; i < graph->vertex_number; i++) { 125 for (j = 0; j < graph->vertex_number; j++) { 126 if (d[i][k] + d[k][j] < d[i][j]) { 127 d[i][j] = d[i][k] + d[k][j]; 128 /* 129 * Find negative circle 130 */ 131 if (i == j && d[i][j] < 0) { 132 return 0; 133 } 134 path[i][j] = k; 135 } 136 } 137 } 138 } 139 return 1; 140 } 141 142 /* 143 * Print the distance matrix 144 */ 145 void toStringDistance(int d[MAX_VERTEX_NUM][MAX_VERTEX_NUM], int length) { 146 vertex i, j; 147 for (i = 0; i < length; i++) { 148 printf("%d:", i); 149 for (j = 0; j < length; j++) { 150 printf("%d ", d[i][j]); 151 } 152 printf("\n"); 153 } 154 } 155 156 /* 157 * Print the path from source to destination 158 * we will recursive method to print the path 159 * 1.find the k between source and destination 160 * 2.find point between source and k and recursive this method until there no points between them 161 * 3.find the point between k and destination and recursive this method until there no points between them 162 * @param source The index of the source point 163 * @param destination The index of the destination 164 * @param path A two-dimensional array to store the path 165 */ 166 void toStringPath(int source, int destination, vertex path[][MAX_VERTEX_NUM]) { 167 if (destination == -1 || source == -1) { 168 return; 169 } else { 170 toStringPath(source, path[source][destination], path); 171 if (path[source][destination] != -1) { 172 printf("%d ", path[source][destination]); 173 } 174 toStringPath(path[source][destination], destination, path); 175 } 176 177 } 178 179 /* 180 * A test method 181 */ 182 int main() { 183 adjacentMatrixGraph graph = buildGraph(1); 184 int d[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 185 int path[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 186 int source = 1; 187 int destination = 5; 188 floyd(graph, d, path); 189 printf("toDistance\n"); 190 toStringDistance(d, graph->vertex_number); 191 printf("toPath:"); 192 printf("%d ", source); 193 toStringPath(source, destination, path); 194 printf("%d", destination); 195 return 0; 196 }
下面是一個練習題:
哈利·波特要考試了,他需要你的幫助。這門課學的是用魔咒將一種動物變成另一種動物的本事。例如將貓變成老鼠的魔咒是haha,將老鼠變成魚的魔咒是hehe等等。反方向變化的魔咒就是簡單地將原來的魔咒倒過來念,例如ahah可以將老鼠變成貓。另外,如果想把貓變成魚,可以通過念一個直接魔咒lalala,也可以將貓變老鼠、老鼠變魚的魔咒連起來念:hahahehe。
現在哈利·波特的手里有一本教材,里面列出了所有的變形魔咒和能變的動物。老師允許他自己帶一只動物去考場,要考察他把這只動物變成任意一只指定動物的本事。於是他來問你:帶什么動物去可以讓最難變的那種動物(即該動物變為哈利·波特自己帶去的動物所需要的魔咒最長)需要的魔咒最短?例如:如果只有貓、鼠、魚,則顯然哈利·波特應該帶鼠去,因為鼠變成另外兩種動物都只需要念4個字符;而如果帶貓去,則至少需要念6個字符才能把貓變成魚;同理,帶魚去也不是最好的選擇。
輸入格式:
輸入說明:輸入第1行給出兩個正整數NNN (≤100\le 100≤100)和MMM,其中NNN是考試涉及的動物總數,MMM是用於直接變形的魔咒條數。為簡單起見,我們將動物按1~NNN編號。隨后MMM行,每行給出了3個正整數,分別是兩種動物的編號、以及它們之間變形需要的魔咒的長度(≤100\le 100≤100),數字之間用空格分隔。
輸出格式:
輸出哈利·波特應該帶去考場的動物的編號、以及最長的變形魔咒的長度,中間以空格分隔。如果只帶1只動物是不可能完成所有變形要求的,則輸出0。如果有若干只動物都可以備選,則輸出編號最小的那只。
輸入樣例:
6 11
3 4 70
1 2 1
5 4 50
2 6 50
5 6 60
1 3 70
4 6 60
3 6 80
5 1 100
2 4 60
5 2 80
輸出樣例:
4 70

1 /* 2 * harrypot.c 3 * 4 * Created on: 2017年5月14日 5 * Author: ygh 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 #define MAX_VERTEX_NUM 100 /*define the max number of the vertex*/ 11 #define INFINITY 65535 /*define double byte no negitive integer max number is 65535*/ 12 #define ERROR -1 13 14 typedef int vertex; /*define the data type of the vertex*/ 15 typedef int weightType; /*define the data type of the weight*/ 16 typedef char dataType; /*define the data type of the vertex value*/ 17 18 /*define the data structure of the Edge*/ 19 typedef struct eNode *ptrToENode; 20 typedef struct eNode { 21 vertex v1, v2; /*two vertex between the edge <v1,v2>*/ 22 weightType weight; /*the value of the edge's weight */ 23 }; 24 typedef ptrToENode edge; 25 26 /*define the data structure of the graph*/ 27 typedef struct gNode *ptrToGNode; 28 typedef struct gNode { 29 int vertex_number; /*the number of the vertex*/ 30 int edge_nunber; /*the number of the edge*/ 31 weightType g[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; /*define the adjacent matrix weight of graph*/ 32 dataType data[MAX_VERTEX_NUM]; /*define the dataType array to store the value of vertex*/ 33 }; 34 typedef ptrToGNode adjacentMatrixGraph; /*a graph show by adjacent matrix*/ 35 36 /* 37 create a graph given the vertex number. 38 @param vertexNum The verter number of the graph 39 @return a graph with vertex but no any egdgs 40 */ 41 adjacentMatrixGraph createGraph(int vertexNum) { 42 vertex v, w; 43 adjacentMatrixGraph graph; 44 graph = (adjacentMatrixGraph) malloc(sizeof(struct gNode)); 45 graph->vertex_number = vertexNum; 46 graph->edge_nunber = 0; 47 /*initialize the adjacent matrix*/ 48 for (v = 0; v < graph->vertex_number; v++) { 49 for (w = 0; w < graph->vertex_number; w++) { 50 graph->g[v][w] = INFINITY; 51 } 52 } 53 54 return graph; 55 } 56 57 /* 58 insert a edge to graph.We will distinct oriented graph and undirected graph 59 @param graph The graph you want to insert edge 60 @param e The edge you want to insert the graph 61 @param isOriented Whether the graph is oriented graph.If the graph is oriented 62 we will set adjacent matrix [n][m]=[m][n]=edge's weight,else we only set 63 the adjacent matrix [n][m]=edge's weight 64 */ 65 void inserEdge(adjacentMatrixGraph graph, edge e, int isOriented) { 66 graph->g[e->v1][e->v2] = e->weight; 67 if (!isOriented) { 68 graph->g[e->v2][e->v1] = e->weight; 69 } 70 } 71 72 /* 73 construct a graph according user's input 74 75 @return a graph has been filled good 76 */ 77 adjacentMatrixGraph buildGraph(int isOrdered) { 78 adjacentMatrixGraph graph; 79 edge e; 80 vertex i; 81 int vertex_num; 82 scanf("%d", &vertex_num); 83 graph = createGraph(vertex_num); 84 scanf("%d", &(graph->edge_nunber)); 85 if (graph->edge_nunber) { 86 e = (edge) malloc(sizeof(struct eNode)); 87 for (i = 0; i < graph->edge_nunber; i++) { 88 scanf("%d %d %d", &e->v1, &e->v2, &e->weight); 89 e->v1--; 90 e->v2--; 91 inserEdge(graph, e, isOrdered); 92 } 93 } 94 return graph; 95 96 } 97 98 int floyd(adjacentMatrixGraph graph, weightType d[][MAX_VERTEX_NUM], 99 vertex path[][MAX_VERTEX_NUM]) { 100 vertex i, j, k; 101 /* 102 * Initialize array 103 */ 104 for (i = 0; i < graph->vertex_number; i++) { 105 for (j = 0; j < graph->vertex_number; j++) { 106 d[i][j] = graph->g[i][j]; 107 path[i][j] = -1; 108 } 109 } 110 for (k = 0; k < graph->vertex_number; k++) { 111 for (i = 0; i < graph->vertex_number; i++) { 112 for (j = 0; j < graph->vertex_number; j++) { 113 if (d[i][k] + d[k][j] < d[i][j]) { 114 d[i][j] = d[i][k] + d[k][j]; 115 /* 116 * Find negative circle 117 */ 118 if (i == j) { 119 d[i][j] = INFINITY; 120 } 121 path[i][j] = k; 122 } 123 } 124 } 125 } 126 return 1; 127 } 128 129 void toStringDistance(int d[MAX_VERTEX_NUM][MAX_VERTEX_NUM], int length) { 130 vertex i, j; 131 for (i = 0; i < length; i++) { 132 printf("%d:", i); 133 for (j = 0; j < length; j++) { 134 printf("%d ", d[i][j]); 135 } 136 printf("\n"); 137 } 138 } 139 140 /* 141 * Algorithms thoughts: 142 * choose the max value expect for 65535 then sore a array,then find the 143 * minimal value from the array and the index 144 */ 145 void selectAnnimal(int d[MAX_VERTEX_NUM][MAX_VERTEX_NUM], int length) { 146 vertex i, j; 147 int maxValue = -1; 148 int minValue = 65535; 149 int key = 0; 150 int arr[length]; 151 for (i = 0; i < length; i++) { 152 maxValue = -1; 153 for (j = 0; j < length; j++) { 154 if (d[i][j] > maxValue && i != j) { 155 maxValue = d[i][j]; 156 } 157 } 158 if (maxValue == INFINITY) { 159 printf("0\n"); 160 return; 161 } else { 162 arr[i] = maxValue; 163 } 164 } 165 166 for (i = 0; i < length; i++) { 167 if (arr[i] < minValue) { 168 key = i; 169 minValue = arr[i]; 170 } 171 } 172 173 printf("%d %d", key + 1, minValue); 174 } 175 176 int main() { 177 adjacentMatrixGraph graph = buildGraph(0); 178 int d[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 179 int path[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; 180 floyd(graph, d, path); 181 selectAnnimal(d, graph->vertex_number); 182 return 0; 183 }
測試數據:
6 11
3 4 70
1 2 1
5 4 50
2 6 50
5 6 60
1 3 70
4 6 60
3 6 80
5 1 100
2 4 60
5 2 80
輸出樣例:
4 70