user-0 提问: 求大佬帮助

回复:
user: user-1
content: C语言版： 之前的方法使用的就是迪杰斯特拉算法 可求出任意两顶点的最短路径  #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include <math.h> #define MAX_NUM 10000001   #define false 0 #define true 1 typedef int bool;  int returnmind(int * a[500], int * b[500], int head, int x, int* seq, bool*final);//找出最短距离 void findx(int first, int N, int * a[500], int*D, int * b[500], int des);  int main() { 	printf("以下所有的输入均使用空格分开\n"); 	char  S1, DES1; 	int N, M, S, DES; 	printf("输入顶点个数和边数：\n" );  	scanf("%d %d",  &N, &M); 	printf("输入出发顶点和目的顶点：\n"); 	scanf("\n%c %c", &S1 ,&DES1);  	S = S1 - 'a'; 	DES = DES1 - 'a'; 	int D[500]; 	int* a[500], *b[500];//距离和费用 	for (int i = 0; i < 500; i++) 	{ 		a[i] = malloc(sizeof(int)* 500); 		b[i] = malloc(sizeof(int)* 500); 	} 	char first, second; 	int point1, point2, length, price = 0;//price没用用 	//初始化a，b 	for (int i = 0; i < 500; i++) 	for (int j = 0; j < 500; j++) 	{ 		a[i][j] = MAX_NUM; 		b[i][j] = MAX_NUM; 	} 	//输入数据 	for (int i = 1; i <= M; i++) 	{ 		printf( "输入第%d条边:\n",i) ; 		scanf("\n%c %c %d", &first,&second , &length); 		point1 = first - 'a'; 		point2 = second - 'a'; 		a[point1][point2] = length; 		a[point2][point1] = length; 		a[point1][point1] = 0; 		a[point2][point2] = 0; 		b[point1][point2] = 0; 		b[point2][point1] = 0; 	} 	findx(S, N, a, D, b, DES);  	//释放内存 	for (int i = 0; i < 500; i++) 	{ 		free (a[i]); 		free (b[i]); 	} 	system("pause"); 	return 0; } //找出最小的D[x] void findx(int first, int N, int * a[500], int*D, int * b[500], int des) { 	int flag = false;//判断是否找到终点，找到则true 	bool final[500]; 	int price[500];//每条最短路径的费用 	for (int i = 0; i < N; i++) 	{ 		final[i] = false; 		price[i] = MAX_NUM; 	} 	final[first] = true; 	for (int k = 0; k < N; k++)//循环次数 	{  		int seq = 0; 		int tmp;  		tmp = returnmind(a, b, first, N, &seq, final);//找到最小的距离 		D[seq] = tmp; 		final[seq] = true; 		price[seq] = b[first][seq]; 		for (int i = 0; i < N; i++) 		if (((D[seq] + a[seq][i]) <= a[first][i]) && !final[i])//如果距离更短的话 		{ 			//更新所有的距离 和费用 			if ((((D[seq] + a[seq][i]) == a[first][i]) && (b[first][seq] + b[seq][i]) < b[first][i]) || (D[seq] + a[seq][i]) < a[first][i]) 			{ 				a[first][i] = D[seq] + a[seq][i]; 				b[first][i] = b[first][seq] + b[seq][i]; 			} 		} 	} 	 	printf("\n最短路径长度：%d\n" , a[first][des] );//输出最短路径 } int returnmind(int * a[500], int * b[500], int head, int x, int *seq, bool*final)//找出最短距离 { 	int min = MAX_NUM; 	int minprice = MAX_NUM; 	for (int j = 0; j < x; j++) 	if (a[head][j] != 0 && !final[j]) 	{ 		if (min >= a[head][j])//路径最短 		{ 			if ((min == a[head][j] && minprice > b[head][j]) || min > a[head][j])//价格最低 			{ 				min = a[head][j]; 				*seq = j; 			} 		} 	} 	return min; }

user: user-0
content: 越简单越好

user: user-0
content: 引用 5 楼 Van_Le的回复:以下程序能完成求最短路径：  #include<iostream> #include <iomanip> #include<string.h> #define MAX_NUM 10000001 using namespace std; int returnmind(int * a[500], int * b[500], int head, int x, int &seq, bool*final);//找出最短距离 void findx(int first, int N, int * a[500], int*D, int * b[500], int des);  int main() { 	cout << "以下所有的输入均使用空格分开" << endl; 	char  S1, DES1; 	int N, M, S, DES; 	cout << "输入顶点个数和边数："<<endl;  	cin >> N >> M; 	cout << "输入出发顶点和目的顶点："<<endl; 	cin>> S1 >> DES1;  	S = S1 - 'a'; 	DES = DES1 - 'a'; 	int D[500]; 	int* a[500], *b[500];//距离和费用 	for (int i = 0; i < 500; i++) 	{ 		a[i] = new int[500]; 		b[i] = new int[500]; 	} 	char first, second; 	int point1, point2, length, price=0;//price没用用 	//初始化a，b 	for (int i = 0; i < 500; i++) 	for (int j = 0; j < 500; j++) 	{ 		a[i][j] = MAX_NUM; 		b[i][j] = MAX_NUM; 	} 	//输入数据 	for (int i = 1; i <= M; i++) 	{ 		cout << "输入第" << i << "条边:" << endl; 		cin >> first >> second >> length; 		point1 = first - 'a'; 		point2 = second - 'a'; 		a[point1][point2] = length; 		a[point2][point1] = length; 		a[point1][point1] = 0; 		a[point2][point2] = 0; 		b[point1][point2] = 0; 		b[point2][point1] = 0; 	} 	findx(S, N, a, D, b, DES);  	//释放内存 	for (int i = 0; i < 500; i++) 	{ 		delete  a[i]; 		delete  b[i]; 	} 	system("pause"); 	return 0; } //找出最小的D[x] void findx(int first, int N, int * a[500], int*D, int * b[500], int des) { 	bool final[500]; 	int price[500];//每条最短路径的费用 	for (int i = 0; i <N; i++) 	{ 		final[i] = false; 		price[i] = MAX_NUM; 	} 	final[first] = true; 	for (int k = 0; k < N; k++)//循环次数 	{  		int seq = 0; 		int tmp;  		tmp = returnmind(a, b, first, N, seq, final);//找到最小的距离 		D[seq] = tmp; 		final[seq] = true; 		price[seq] = b[first][seq]; 		for (int i = 0; i < N; i++) 		if (((D[seq] + a[seq][i]) <= a[first][i]) && !final[i])//如果距离更短的话 		{ 			//更新所有的距离 和费用 			if ((((D[seq] + a[seq][i]) == a[first][i]) && (b[first][seq] + b[seq][i]) < b[first][i]) || (D[seq] + a[seq][i]) < a[first][i]) 			{ 				a[first][i] = D[seq] + a[seq][i]; 				b[first][i] = b[first][seq] + b[seq][i]; 			} 		} 	} 	cout << endl; 	cout << "最短路径长度："<<a[first][des]  << endl;//输出最短路径 } int returnmind(int * a[500], int * b[500], int head, int x, int &seq, bool*final)//找出最短距离 { 	int min = MAX_NUM; 	int minprice = MAX_NUM; 	for (int j = 0; j < x; j++) 	if (a[head][j] != 0 && !final[j]) 	{ 		if (min >= a[head][j])//路径最短 		{ 			if ((min == a[head][j] && minprice > b[head][j]) || min > a[head][j])//价格最低 			{ 				min = a[head][j]; 				seq = j; 			} 		} 	} 	return min; }   结果：  大哥，我就是一萌新，c都没学好，你给我个c++的程序我也看不懂啊，而且这程序我们老师一看就知道不是我写的啊，求大佬给个基于floyed或者迪杰斯特拉算法的程序，最好能求出任意两点的最短路径，以及最短路径经过的点

user: user-0
content: 谢谢大佬，程序我运行了下，可以运行，结果也对。

user: user-2
content: 图片刚刚好像没放上去，这次再试试

user: user-2
content: 这是我的运行结果，楼主可以看看符合要求不

user: user-2
content: 楼上的程序我运行了下，出了6个错误，改改应该能运行，我这写了个程序，已经运行成功，楼主可以试试，望采纳。 #include "stdio.h"  #include "stdlib.h"  #include "io.h"  #include "math.h"  #include "time.h"  #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define MAXEDGE 20 #define MAXVEX 20 #define INFINITY 65535  typedef int Status;    /* Status是函数的类型,其值是函数结果状态代码，如OK等 */  typedef struct {     int vexs[MAXVEX];     int arc[MAXVEX][MAXVEX];     int numVertexes, numEdges; }MGraph;  typedef int Patharc[MAXVEX][MAXVEX]; typedef int ShortPathTable[MAXVEX][MAXVEX];  /* 构件图 */ void CreateMGraph(MGraph *G) {     int i,j,k,w; 	printf("输入顶点数和边数:\n"); 	scanf("%d,%d",&G->numVertexes,&G->numEdges); /* 输入顶点数和边数 */ 	for(i = 0;i <G->numVertexes;i++) /* 读入顶点信息,建立顶点表 */ 	{ 		printf("输入图的顶点\n"); 		fflush(stdin);//清空输入缓冲区，为了确保不影响后面的数据读取 		scanf("%c",&G->vexs[i]); 		getchar(); 	}         for (i=0; i<G->numVertexes;i++)/* 初始化图 */     {         G->vexs[i]=i;     } printf("断\n");     for (i=0; i<G->numVertexes;i++)/* 初始化图 */     {         for ( j=0;j<G->numVertexes;j++)         {             if (i==j)                 G->arc[i][j]=0;             else                 G->arc[i][j] = G->arc[j][i] = INFINITY;         } 	     } for(k=0;k<G->numEdges;k++) { printf("输入边（Vi，Vj）上的下标i，下标j和权w：\n"); scanf("%d,%d,%d",&i,&j,&w); getchar(); G->arc[i][j]=w; } 	printf("断\n");        }  /* Floyd算法，求网图G中各顶点v到其余顶点w的最短路径P[v][w]及带权长度D[v][w]。 */  void ShortestPath_Floyd(MGraph G, Patharc *P, ShortPathTable *D) {      int v,w,k;      for(v=0; v<G.numVertexes; ++v) /* 初始化D与P */      {          for(w=0; w<G.numVertexes; ++w)          {             (*D)[v][w]=G.arc[v][w];    /* D[v][w]值即为对应点间的权值 */             (*P)[v][w]=w;                /* 初始化P */         }     }     for(k=0; k<G.numVertexes; ++k)      {         for(v=0; v<G.numVertexes; ++v)          {              for(w=0; w<G.numVertexes; ++w)              {                 if ((*D)[v][w]>(*D)[v][k]+(*D)[k][w])                 {/* 如果经过下标为k顶点路径比原两点间路径更短 */                     (*D)[v][w]=(*D)[v][k]+(*D)[k][w];/* 将当前两点间权值设为更小的一个 */                     (*P)[v][w]=(*P)[v][k];/* 路径设置为经过下标为k的顶点 */                 }             }         }     } }  int main(void) {      int v,w,k;      MGraph G;           Patharc P;      ShortPathTable D; /* 求某点到其余各点的最短路径 */           CreateMGraph(&G);          ShortestPath_Floyd(G,&P,&D);       printf("各顶点间最短路径如下:\n");      for(v=0; v<G.numVertexes; ++v)      {          for(w=v+1; w<G.numVertexes; w++)          {             printf("v%d-v%d weight: %d ",v,w,D[v][w]);             k=P[v][w];                /* 获得第一个路径顶点下标 */             printf(" path: %d",v);    /* 打印源点 */             while(k!=w)                /* 如果路径顶点下标不是终点 */             {                 printf(" -> %d",k);    /* 打印路径顶点 */                 k=P[k][w];            /* 获得下一个路径顶点下标 */             }             printf(" -> %d\n",w);    /* 打印终点 */         }         printf("\n");     }      printf("最短路径D\n");     for(v=0; v<G.numVertexes; ++v)      {          for(w=0; w<G.numVertexes; ++w)          {             printf("%d\t",D[v][w]);         }         printf("\n");     }     printf("最短路径P\n");     for(v=0; v<G.numVertexes; ++v)      {          for(w=0; w<G.numVertexes; ++w)          {             printf("%d ",P[v][w]);         }         printf("\n");     }      return 0; }

user: user-1
content: 以下程序能完成求最短路径：  #include<iostream> #include <iomanip> #include<string.h> #define MAX_NUM 10000001 using namespace std; int returnmind(int * a[500], int * b[500], int head, int x, int &seq, bool*final);//找出最短距离 void findx(int first, int N, int * a[500], int*D, int * b[500], int des);  int main() { 	cout << "以下所有的输入均使用空格分开" << endl; 	char  S1, DES1; 	int N, M, S, DES; 	cout << "输入顶点个数和边数："<<endl;  	cin >> N >> M; 	cout << "输入出发顶点和目的顶点："<<endl; 	cin>> S1 >> DES1;  	S = S1 - 'a'; 	DES = DES1 - 'a'; 	int D[500]; 	int* a[500], *b[500];//距离和费用 	for (int i = 0; i < 500; i++) 	{ 		a[i] = new int[500]; 		b[i] = new int[500]; 	} 	char first, second; 	int point1, point2, length, price=0;//price没用用 	//初始化a，b 	for (int i = 0; i < 500; i++) 	for (int j = 0; j < 500; j++) 	{ 		a[i][j] = MAX_NUM; 		b[i][j] = MAX_NUM; 	} 	//输入数据 	for (int i = 1; i <= M; i++) 	{ 		cout << "输入第" << i << "条边:" << endl; 		cin >> first >> second >> length; 		point1 = first - 'a'; 		point2 = second - 'a'; 		a[point1][point2] = length; 		a[point2][point1] = length; 		a[point1][point1] = 0; 		a[point2][point2] = 0; 		b[point1][point2] = 0; 		b[point2][point1] = 0; 	} 	findx(S, N, a, D, b, DES);  	//释放内存 	for (int i = 0; i < 500; i++) 	{ 		delete  a[i]; 		delete  b[i]; 	} 	system("pause"); 	return 0; } //找出最小的D[x] void findx(int first, int N, int * a[500], int*D, int * b[500], int des) { 	bool final[500]; 	int price[500];//每条最短路径的费用 	for (int i = 0; i <N; i++) 	{ 		final[i] = false; 		price[i] = MAX_NUM; 	} 	final[first] = true; 	for (int k = 0; k < N; k++)//循环次数 	{  		int seq = 0; 		int tmp;  		tmp = returnmind(a, b, first, N, seq, final);//找到最小的距离 		D[seq] = tmp; 		final[seq] = true; 		price[seq] = b[first][seq]; 		for (int i = 0; i < N; i++) 		if (((D[seq] + a[seq][i]) <= a[first][i]) && !final[i])//如果距离更短的话 		{ 			//更新所有的距离 和费用 			if ((((D[seq] + a[seq][i]) == a[first][i]) && (b[first][seq] + b[seq][i]) < b[first][i]) || (D[seq] + a[seq][i]) < a[first][i]) 			{ 				a[first][i] = D[seq] + a[seq][i]; 				b[first][i] = b[first][seq] + b[seq][i]; 			} 		} 	} 	cout << endl; 	cout << "最短路径长度："<<a[first][des]  << endl;//输出最短路径 } int returnmind(int * a[500], int * b[500], int head, int x, int &seq, bool*final)//找出最短距离 { 	int min = MAX_NUM; 	int minprice = MAX_NUM; 	for (int j = 0; j < x; j++) 	if (a[head][j] != 0 && !final[j]) 	{ 		if (min >= a[head][j])//路径最短 		{ 			if ((min == a[head][j] && minprice > b[head][j]) || min > a[head][j])//价格最低 			{ 				min = a[head][j]; 				seq = j; 			} 		} 	} 	return min; }   结果：

user: user-0
content: 输入顶点只能是字母【size=14px】scanf("%c",&G->vexs[i]);[/size] 还有就是我这一段写错了scanf("%c,%c,%d",&i,&j,&w); /* 输入边(vi,vj)上的权w */ 应该是scanf("%d,%d,%d",&i,&j,&w); /* 输入边(vi,vj)上的权w */

user: user-1
content: 问下你输入的 顶点的 只能是字母么？  如果只能是字母，得花点时间改，数字就好说了。

user: user-0
content: 那一段我写错了，应该是%d形式的，不过该了后还是没用

user: user-1
content: 你的顶点输入，只能是字母？可否是数字？