#include <iostream>
#include "Dijkstra.h"
using namespace std;
int main()
{
cout << "請輸入二維數組(k*k)中k值:";
int k;
cin >> k;
int**p = new int*[k];
for(int i = 0; i < k; ++i)
{
p[i] = new int[k];
}
cout << "請輸入二維數組中的元素(100表示無窮大):";
for(int i = 0; i < k; ++i)
{
for(int j = 0; j < k; ++j)
{
cin >> p[i][j];
}
}
bool* S = new bool[k];
int* D = new int[k];
int* Par = new int[k];
int s = 0;
cout << "請輸入源點:";
cin >> s;
Dijkstra(p, s, S, D, Par, k);
for(int d = 0; d < k; ++d)
{
display(s, d, D, Par, k);
}
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
#define INFINITE 100
bool emptyBlue(bool S[], int size);
void Dijkstra(int** p, int s, bool S[], int D[], int Par[], int size);
int chooseBlue(bool S[], int D[], int size);
void relax(int** p, bool S[], int D[], int Par[], int size);
void addRed(bool S[], int n);
int getVertex(int** p, int s, int size);
void initializeS(bool S[], int s, int size);
void initializePra(int Par[], int** p, int s, int size);
void initializeD(int** p, int D[], int s, int size);
string route(int s, int d, int Par[], int size);
string reverse(string s);
void display(int s, int d, int D[], int Par[], int size);
void Dijkstra(int** p, int s, bool S[], int D[], int Par[], int size)
{
initializeS(S, s, size);
initializePra(Par, p, s, size);
initializeD(p, D, s, size);
while(!emptyBlue(S, size))
{
int nextRed = getVertex(p, s, size);
addRed(S, nextRed);
relax(p, S, D, Par, size);
nextRed = chooseBlue(S, D, size);
addRed(S, nextRed);
}
}
bool emptyBlue(bool S[], int size)
{
for(int i = 0; i < size; ++i)
{
if (S[i] == false)
{
return false;
}
}
return true;
}
int chooseBlue(bool S[], int D[], int size)
{
int min = INFINITE;
int index = 0;
for(int i = 0; i < size; ++i)
{
if (S[i] == false && D[i] < min)
{
index = i;
min = D[i];
}
}
return index;
}
void relax(int** p, bool S[], int D[], int Par[], int size)
{
for (int i = 0; i < size; ++i)
{
if (S[i] == true)
{
for(int j = 0; j < size; ++j)
{
if (S[j] == false)
{
if (D[i] + p[i][j] < D[j])
{
Par[j] = i;
D[j] = D[i] + p[i][j];
}
}
}
}
}
}
void addRed(bool S[], int n)
{
S[n] = true;
}
int getVertex(int** p, int s, int size)
{
int min = INFINITE;
int index = 0;
for(int i = 0; i < size; ++i)
{
if (i == s)
{
continue;
}
if(p[s][i] < min)
{
min = p[s][i];
index = i;
}
}
return index;
}
void initializeS(bool S[], int s, int size)
{
for(int i = 0; i < size; ++i)
{
if (i == s)
{
S[i] = true;
}
else
{
S[i] = false;
}
}
}
void initializePra(int Par[], int** p, int s, int size)
{
for(int i = 0; i < size; ++i)
{
if (i == s)
{
Par[i] = -1;
}
else
{
if (p[s][i] < INFINITE)
{
Par[i] = s;
}
else
{
Par[i] = -1;
}
}
}
}
void initializeD(int** p, int D[], int s, int size)
{
for(int i = 0; i < size; ++i)
{
D[i] = p[s][i];
}
}string route(int s, int d, int Par[], int size)
{
int* t = new int[size];
for(int i = 0; i < size; ++i)
{
t[i] = Par[i];
}
string str = "";
char* c = new char;
if (Par[s] == t[d])
{
itoa(s, c, 10);
str += c;
}
else
{
itoa(d, c, 10);
str += c;
while(Par[s] != t[d])
{
itoa(t[d], c, 10);
str += c;
int k = t[d];
t[d] = Par[k];
//str += " ";
}
}
}
string reverse(string s)
{
int k = s.length();
string s1 = "";
for(int i = 0; i < k; ++i)
{
s1 += s[k - i - 1];
}
return s1;
}
void display(int s, int d, int D[], int Par[], int size)
{
string str = reverse(route(s, d, Par, size));
cout << s << "---->" << d << " 最短路徑為" << D[d] << " 父親為"
<< Par[d] << " 路徑為:" << str << endl;
}