主题:帮忙看看这个Dijkstra算法问题出在什么地方?
#include<iostream.h>
#include<stdio.h>
#include<string.h>
const int INFINITY=10000;
typedef struct
{
int **cost;
int vertex_num;
int edge_num;
}Graph;
void CreatGraph(Graph &g) //构造邻接矩阵,这里没什么问题
{
int n,first,second;
cout<<"Input the number of edges: ";
cin>>g.edge_num;
cout<<"Input the number of vertexs:";
cin>>g.vertex_num;
g.cost=new int *[g.vertex_num];
for(n=0;n<g.vertex_num;n++)
g.cost[n]=new int[g.vertex_num];
for(first=0;first<g.vertex_num;first++)
for(second=0;second<g.vertex_num;second++)
g.cost[first][second]=INFINITY;
for(n=0;n<g.edge_num;n++)
{
cout<<"Input the begining serial number of edge"<<n+1<<":";
cin>>first;
cout<<"Input the ending serial number of edge"<<n+1<<":";
cin>>second;
cout<<"Input the edge'cost:";
cin>>g.cost[first][second];
cout<<endl;
}
}
void Dijkstra(Graph g,int v)
{
int *dist,*S;
char **path,temp[20];
int i,j,min,k;
dist=new int[g.vertex_num];
S=new int[g.vertex_num];
path=new char *[g.vertex_num];
for(i=0;i<g.vertex_num;i++)
path[i]=new char[100];
for(i=0;i<g.vertex_num;i++)
{
dist[i]=g.cost[v][i];
if(dist[i]=INFINITY)
sprintf(path[i],"%d→%d",v,i);
else
strcpy(path[i],"");
S[i]=0;
}
S[v]=1;
for(i=1;i<g.vertex_num;i++)
{
min=INFINITY;
j=0;
for(k=0;k<g.vertex_num;k++)
if(!S[k] && dist[k]<min)
{
min=dist[k];
j=k;
}
S[j]=1;
for(k=0;k<g.vertex_num;k++)
if(!S[k] && dist[j]+g.cost[j][k]<dist[k])
{
dist[k]= dist[j]+g.cost[j][k];
sprintf(temp,"→%d",k++);
strcpy(path[k],path[j]);
strcat(path[k],temp);
}
}
for(i=0;i<g.vertex_num;i++)
cout<<dist[i]<<" "<<path[i]<<endl;
delete []dist;
delete []S;
for(i=0;i<g.vertex_num;i++)
delete []path[i];
delete []path;
}
void main()
{
int v=0;
Graph g;
CreatGraph(g);
Dijkstra(g,v);
}
运行时采用的邻接矩阵是
v0 v1 v2 v3 v4 //&表示无穷大
v0 & 10 99 & &
v1 & & 20 & &
v2 & & & 60 20
v3 & & & & &
v4 & & & 10 &
但运行的结果是
10000 0->1
10000 0->2
10000 0->3
10000 0->4
弄了好久,就不知道是怎么回事,哪里出了问题,帮忙看看好吗?
谢谢啦!
#include<stdio.h>
#include<string.h>
const int INFINITY=10000;
typedef struct
{
int **cost;
int vertex_num;
int edge_num;
}Graph;
void CreatGraph(Graph &g) //构造邻接矩阵,这里没什么问题
{
int n,first,second;
cout<<"Input the number of edges: ";
cin>>g.edge_num;
cout<<"Input the number of vertexs:";
cin>>g.vertex_num;
g.cost=new int *[g.vertex_num];
for(n=0;n<g.vertex_num;n++)
g.cost[n]=new int[g.vertex_num];
for(first=0;first<g.vertex_num;first++)
for(second=0;second<g.vertex_num;second++)
g.cost[first][second]=INFINITY;
for(n=0;n<g.edge_num;n++)
{
cout<<"Input the begining serial number of edge"<<n+1<<":";
cin>>first;
cout<<"Input the ending serial number of edge"<<n+1<<":";
cin>>second;
cout<<"Input the edge'cost:";
cin>>g.cost[first][second];
cout<<endl;
}
}
void Dijkstra(Graph g,int v)
{
int *dist,*S;
char **path,temp[20];
int i,j,min,k;
dist=new int[g.vertex_num];
S=new int[g.vertex_num];
path=new char *[g.vertex_num];
for(i=0;i<g.vertex_num;i++)
path[i]=new char[100];
for(i=0;i<g.vertex_num;i++)
{
dist[i]=g.cost[v][i];
if(dist[i]=INFINITY)
sprintf(path[i],"%d→%d",v,i);
else
strcpy(path[i],"");
S[i]=0;
}
S[v]=1;
for(i=1;i<g.vertex_num;i++)
{
min=INFINITY;
j=0;
for(k=0;k<g.vertex_num;k++)
if(!S[k] && dist[k]<min)
{
min=dist[k];
j=k;
}
S[j]=1;
for(k=0;k<g.vertex_num;k++)
if(!S[k] && dist[j]+g.cost[j][k]<dist[k])
{
dist[k]= dist[j]+g.cost[j][k];
sprintf(temp,"→%d",k++);
strcpy(path[k],path[j]);
strcat(path[k],temp);
}
}
for(i=0;i<g.vertex_num;i++)
cout<<dist[i]<<" "<<path[i]<<endl;
delete []dist;
delete []S;
for(i=0;i<g.vertex_num;i++)
delete []path[i];
delete []path;
}
void main()
{
int v=0;
Graph g;
CreatGraph(g);
Dijkstra(g,v);
}
运行时采用的邻接矩阵是
v0 v1 v2 v3 v4 //&表示无穷大
v0 & 10 99 & &
v1 & & 20 & &
v2 & & & 60 20
v3 & & & & &
v4 & & & 10 &
但运行的结果是
10000 0->1
10000 0->2
10000 0->3
10000 0->4
弄了好久,就不知道是怎么回事,哪里出了问题,帮忙看看好吗?
谢谢啦!