作爲一個無腦莽撞的少年,完成這次(超簡單的)作業歷經波折。
目標是用函數實現,找到圖中每個點到源所有的加權最短路徑,記錄下總長度(邊的權值和)和條數。如果不相連,總長度爲-1和條數0
dist記錄總長度,count記錄找到了幾條這樣的路徑。
用Dijkstra算法可以直接實現非常簡單,但是我太心急和浮躁,沒有徹底弄清楚D算法就直接上手,導致繞了遠路。
不過,也學到了很重要的東西:
1.寫代碼前一定要搞清楚算法,
2.學習數據結構不能聽聽課就好,課下要花功夫鑽研。
3.發現自己的算法改起來很困難的時候,直接推翻換一個思路。
4.寫同一個代碼超過兩個小時就去做點別的事情,回來可以直接推翻重寫。
【知識點複習】:用矩陣表示有向圖,行代表出發的結點,列代表進入的節點
【錯誤總結】:
如果要用Queue來實現Dijkstra算法,需要用最小堆纔可以(((非常麻煩所以在數據量很小的時候就用數組儲存吧
用兩次D算法(參考網上代碼,後來發現可以合併成一次)和用一次D算法中有很多細節錯誤(if語句括號沒括全),但最重要的問題是,缺少Graph->G[V][W]!= INFINITY
然而題目並沒有說過這種事情((
下面放上最後的代碼
void ShortestDist(MGraph Graph, int dist[], int count[], Vertex S){
bool isVisit[MaxVertexNum];
int i, minDis,V, W, tmpDis;
for (i = 0; i < Graph->Nv; i++){
dist[i] = -1;
count[i] = 0;
isVisit[i] = false;
}
dist[S] = 0;
count[S] = 1;
while (1){
minDis = INFINITY;
for (i = 0; i < Graph->Nv; i++){
if (!isVisit[i] && dist[i] < minDis && dist[i] != -1){
V = i;
minDis = dist[V];
}
}
if (minDis == INFINITY) break;
isVisit[V] = true;
for (W = 0; W < Graph->Nv; W++){
tmpDis = dist[V] + Graph->G[V][W];
if(isVisit[W] == false && Graph->G[V][W] >= 0 && Graph->G[V][W] != INFINITY){
if( (dist[V] + Graph->G[V][W] < dist[W]||dist[W] == -1)){
dist[W] = tmpDis;
count[W] = count[V];
}
else if (dist[V] + Graph->G[V][W] == dist[W]){
count[W] += count[V];
}
}
}
}
}
下面爲題目
4-1 Shortest Path [3] (25分)
Write a program to not only find the weighted shortest distances, but also count the number of different minimum paths from any vertex to a given source vertex in a digraph. It is guaranteed that all the weights are positive.
Format of functions:
void ShortestDist( MGraph Graph, int dist[], int count[], Vertex S );
where MGraph
is defined as the following:
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;
The shortest distance from V
to the source S
is
supposed to be stored in dist[V]
. If V
cannot
be reached from S
, store -1 instead. The number of different
minimum paths from V
to the source S
is
supposed to be stored in count[V]
and count[S]=1
.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h>
typedef enum {false, true} bool;
#define INFINITY 1000000
#define MaxVertexNum 10 /* maximum number of vertices */
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */
typedef int WeightType;
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;
MGraph ReadG(); /* details omitted */
void ShortestDist( MGraph Graph, int dist[], int count[], Vertex S );
int main()
{
int dist[MaxVertexNum], count[MaxVertexNum];
Vertex S, V;
MGraph G = ReadG();
scanf("%d", &S);
ShortestDist( G, dist, count, S );
for ( V=0; V<G->Nv; V++ )
printf("%d ", dist[V]);
printf("\n");
for ( V=0; V<G->Nv; V++ )
printf("%d ", count[V]);
printf("\n");
return 0;
}
/* Your function will be put here */
Sample Input (for the graph shown in the figure):
8 11
0 4 5
0 7 10
1 7 30
3 0 40
3 1 20
3 2 100
3 7 70
4 7 5
6 2 1
7 5 3
7 2 50
3
Sample Output:
40 20 100 0 45 53 -1 50
1 1 4 1 1 3 0 3