C++floyd算法最短路径输出有问题
#include<iostream>//#include <string.h>
//#include <string>
//#include <malloc.h>
#include<fstream>
using namespace std;
char *CITY_NAME[] = {"a", "b", "c", "d", "e", "f"};
const int CITY_NUM = 6;
int CITY_PATH = 0;
int SHORT_LENGTH = 0;
bool P[CITY_NUM][CITY_NUM][CITY_NUM];
int D[CITY_NUM][CITY_NUM];
const int INFINITY = 10000;
typedef struct ArcCell {
int adj; //两个城市之间的路径长度,若无联通关系,则用∞表示
//int timeCost; //时间花销
//int expend; //金钱支出
}ArcCell, AdjMatrix;//[MAX_CITY_NUM][MAX_CITY_NUM];
typedef struct {
char *vexs[CITY_NUM]; //顶点向量
AdjMatrix arcs[CITY_NUM][CITY_NUM]; //邻接矩阵
int vexnum, arcnum; //图的当前定点数和边数
} MGragh;
//**********************************************************************//
int LocateVex(MGragh &G, char *v) {
int i = 0;
for(; i < G.vexnum; i++) {
if(!strcmp(G.vexs[i], v))
return i;
}
/*if(i == G.vexnum) {
char temp[5];
cout<<"输入有误,请核实后再次输入!"<<endl;
cin>>temp;
LocateVex(G, temp);
}*/
}
void CreateUDN(MGragh &G) { //初始化邻接矩阵
G.vexnum = CITY_NUM;
cout<<"纳入规划的城市有: ";
ifstream in("juzhen.txt");
if(!in) {
cout<<"无法打开文件. \n";
return ;
}
for(int i = 0; i < G.vexnum; i++) {
//cout<<"输入城市的名称:\t";
cout<<CITY_NAME[i]<<"\t";
G.vexs[i] = CITY_NAME[i];
}
//cout<<"\n请输入路径个数:\t";
//cin>>CITY_PATH;
in>>CITY_PATH;
G.arcnum = CITY_PATH;
//cout<<*G.vexs;//构造顶点向量
for(int m = 0; m < G.vexnum; m++) {
for(int j = 0; j < G.vexnum; j++)
G.arcs[m][j].adj = INFINITY;
G.arcs[m][m].adj = 0;
}
//G.arcs[m][j] = {INFINITY};
for(int k = 0; k < G.arcnum; k++) {
char v1[5], v2[5];
//int length;
// cout<<"\n输入两个城市的名称及其之间的路径长度"<<endl;
//cin>>v1;
in>>v1;
int i = LocateVex(G, v1);
//cin>>v2;
in>>v2;
int j = LocateVex(G, v2); //确定v1,v2在图中的位置
//cout<<"1"<<i<<endl;
//length;
//cin>>G.arcs[i][j].adj;
in>>G.arcs[i][j].adj;
G.arcs[j][i] = G.arcs[i][j];
}
in.close();
}
//***********************************************************************//
void Shortest_Path_FLOYD(MGragh G) {
for(int v = 0; v < G.vexnum; ++v){ //对各结点之间初始已知路径及距离
for(int w = 0; w < G.vexnum; w++) {
D[v][w] = G.arcs[v][w].adj;
for(int u = 0; u < G.vexnum; u++)
P[v][w][u] = false;
if(D[v][w] < INFINITY) { //从v到w有直接路径
P[v][w][v] = true; P[v][w][w] = true;
} //if
} //for
}
for(int u = 0; u < G.vexnum; u++){ //初始化
for(int v = 0; v < G.vexnum; v++){
for(int w = 0; w < G.vexnum; w++)
if(D[v][u] + D[u][w] < D[v][w]) {
D[v][w] = D[v][u] + D[u][w];
for(int i = 0; i < G.vexnum; i++)
P[v][w][i] = P[v][u][i] || P[u][w][i];
} //if
}
}
}
//递归求最短路径 ------------------------
void Search_Shortest_Path(MGragh G,int a, int b, ofstream &out) { //1 ------------------------
int k ; //2
for (k = 0; k < G.vexnum; k++) { //3 if((P[a][b][k] == true) && (k != a) && (k != b)){ //4
Search_Shortest_Path(G, a, k, out); //5
cout<<CITY_NAME[k]<<"—> "; //6 本行的输出会重复执行
out<<CITY_NAME[k]<<"—> "; //7
Search_Shortest_Path(G, k, b, out); //8
} //if
} //for
} ------------------------
//**********************************************************************// ------------------------
int main() {
int i;
char from[5], end[5];
int m[CITY_NUM] = {-1};
MGragh G;
CreateUDN(G);
Shortest_Path_FLOYD(G);
ofstream out("path.txt");
cout<<"\n选择路线请按1,退出请按0:"<<endl;
out<<"\n选择路线请按1,退出请按0:"<<endl;
cin>>i;
out<<i;
while(i != 0) {
cout<<"\n输入始发地及目的地"<<endl;
out<<"\n输入始发地及目的地"<<endl;
cin>>from;
out<<from;
int m = LocateVex(G, from);
cin>>end;
out<<end;
int n = LocateVex(G, end);
cout<<"从"<<G.vexs[m]<<"到"<<G.vexs[n]<<"的最佳行驶路线为:"<<endl;
out<<"从"<<G.vexs[m]<<"到"<<G.vexs[n]<<"的最佳行驶路线为:"<<endl;
cout<<G.vexs[m]<<"—> ";
out<<G.vexs[m]<<"—> ";
Search_Shortest_Path(G, m, n, out);
cout<<G.vexs[n]<<endl;
out<<G.vexs[n]<<endl;
cout<<"\n总路程为: "<<D[m][n]<<endl;
out<<"\n总路程为: "<<D[m][n]<<endl;
cout<<"选择路线请按1,退出请按0:"<<endl;
out<<"选择路线请按1,退出请按0:"<<endl;
cin>>i;
out<<i;
}
out.close();
return 0;
}
求大神解答,不胜感激!!!!!!!!!!!!!!!