Shortest Path algorithm
#include <iostream>
void path () {
//val[i][j] Distance from I point to J Point, if unreachable, set to 0
int val[8][8];
//res[i][j] The shortest distance from the I point to the J Point, we just get res[0][7]
int res[8][8];
//fa[i]i the last shortest distance point of a point
int fa[8];
for (int i = 0; i<8; i++) {
for (int j = 0; j<8; j + +) {
VAL[I][J] = 0;
RES[I][J] = 10000;
}
Fa[i] =-1;
}
Res[0][0] = 0;
VAL[0][1] = 13;
VAL[0][2] = 15;
VAL[0][3] = 16;
VAL[1][4] = 18;
VAL[1][5] = 17;
VAL[1][6] = 16;
VAL[2][4] = 18;
VAL[2][5] = 20;
VAL[2][6] = 30;
VAL[3][4] = 11;
VAL[3][5] = 9;
VAL[3][6] = 13;
VAL[4][7] = 16;
VAL[5][7] = 14;
VAL[6][7] = 17;
for (int i = 1; i<8; i++) {
for (int j = 0; j<i; j + +) {
if (val[j][i]! = 0) {
if (Res[0][j] +val[j][i]<res[0][i]) {
Res[0][i] = res[0][j] +val[j][i];
Fa[i] = j;
}
}
}
}
int point = 7;
Std::cout << 7 << "";
while (Fa[point]! =-1) {
Std::cout << "<---" << fa[point];
Point = Fa[point];
}
Std::cout << Std::endl;
Std::cout << Res[0][7] <<std::endl;
}
int Main (intargc, const Char * argv[]) {
//Insert code here ...
Std::cout << "hello,world!\n";
Path ();
System ("pause");
return 0;
}
Operation Result:
Shortest Path algorithm