请教如何使用数据结构图的相关理论编程,课程设计(希望能附上结果图)
发布网友
发布时间:2022-04-25 00:01
我来回答
共1个回答
热心网友
时间:2023-10-16 05:39
#include <iostream>
using namespace std;
char cityName[7][256];
int dis[7][7] = {{-1, 100, 200, 200, -1, -1, -1},
{-1, -1, -1, 50, 150, -1, -1},
{-1, -1, -1, 100, -1, 150, -1},
{-1, -1, -1, -1, 100, 350, -1},
{-1, -1, -1, -1, -1, -1, 150},
{-1, -1, -1, -1, 400, -1, 500},
{-1, -1, -1, -1, -1, -1, -1}};
int from[7];
int disSum[7];
int count, count1;
int nextNoList[7];
int nextNoList1[7];
char path[7][256];
char temp[256];
int main()
{
strcpy(cityName[0], "广州");
strcpy(cityName[1], "佛山");
strcpy(cityName[2], "肇庆");
strcpy(cityName[3], "珠海");
strcpy(cityName[4], "深圳");
strcpy(cityName[5], "南宁");
strcpy(cityName[6], "香港");
int i, j;
for (i = 0; i < 7; i++)
{
disSum[i] = -1;
from[i] = -1;
}
count = 1;
nextNoList[0] = 0;
disSum[0] = 0;
strcpy(path[0], cityName[0]);
while (count)
{
count1 = 0;
for (j = 0; j < count; j++)
{
for (i = 1; i < 7; i++)
{
if (dis[nextNoList[j]][i] != -1)
{
if (disSum[i] == -1 || disSum[i] > disSum[nextNoList[j]] + dis[nextNoList[j]][i])
{
disSum[i] = disSum[nextNoList[j]] + dis[nextNoList[j]][i];
nextNoList1[count1++] = i;
from[i] = nextNoList[j];
strcpy(temp, path[nextNoList[j]]);
strcat(temp, " -> ");
strcat(temp, cityName[i]);
strcpy(path[i], temp);
}
}
}
}
count = count1;
for (j = 0; j < count1; j++)
{
nextNoList[j] = nextNoList1[j];
}
}
for (i = 1; i < 7; i++)
{
printf("从%s到%s : %d : %s\n", cityName[0], cityName[i], disSum[i], path[i]);
}
return 0;
}追问有结果图吗?我VS有毒。。。
追答