帮忙看看,输出结果有问题,应该怎么改正

来源:百度知道 编辑:UC知道 时间:2024/05/31 10:35:02
#include<iostream.h>
#define MAXLEN 1000
int cost[7][7]; //图的邻接数组
int dist[7]; //路径长度数组

void creategraph(int *node,int num) //建立加权图
{
int from; //边的起点
int to; //边的终点
int i;
for(i=0;i<num;i++) //读取边的回路
{
from=node[i*3];
to=node[i*3+1];
cost[from][to]=node[i*3+2]; //存入图
cost[to][from]=node[i*3+2];
}
}

void shortestpath(int begin,int num) //找寻某顶点到各顶点的最短距离
{
int selected[7];
int min;
int s; //最短距离的顶点
int i,j;
for(i=2;i<=num;i++)
{
selected[i]=0;
dist[i]=cost[begin][i]; //初始距离
}
selected[begin]=1; //设定找过开始顶点
dist[begin]=0; //设定开始顶点距离
cout<<"顶点1 2 3 4 5 6"<<endl;
for(j=1;j<=num;j++)
cout<< dist[j];
cout<<endl;
for(i=1;i<=num;i++)
{
min=MAXLEN; //先设最长距离
for(j=1;j<=n

因为你的程序中cost[i][j]=MAXLEN; //设定数组最长距离 将所有没有赋值的数组成员都赋值为1000,你一起打出来的时候当然就是全0了

给你改了一下
#include<iostream.h>
#define MAXLEN 1000
int cost[7][7]; //?的?接数?
int dist[7]; //路径?度数?

void creategraph(int *node,int num) //建立加??
{
int from; //?的起点
int to; //?的?点
int i;
for(i=0;i<num;i++) //?取?的回路
{
from=node[i*3];
to=node[i*3+1];
cost[from][to]=node[i*3+2]; //存入?
cost[to][from]=node[i*3+2];
}
}

void shortestpath(int begin,int num) //找?某?点到各?点的最短距?
{
int selected[7];
int min;
int s; //最短距?的?点
int i,j;
for(i=2;i<=num;i++)
{
selected[i]=0;
dist[i]=cost[begin][i]; //初始距?
}
selected[begin]=1; //?定找??始?点
dist[begin]=0; //?定?始?点距?
cout<<"?点1 2 3 4 5 6"<<endl;
for(j=1;j<=num;j++)
{if(dist[j] != 1000)
cout<< dist[j];}
cout<<endl; <