主题:求助~~为什么已输入数据就得不到正确结果
#include<stdio.h>
#define INF 1000
#define MAX_VERTEX_NUM 20
#define true 1
#define false 0
typedef enum{DG,DN,UDG,UDN}GraphKind;
typedef int VRType;
typedef int VertexType;
typedef char InfoType;
typedef struct ArcCell{
VRType adj;
InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
typedef int PathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef int ShortPathTable[MAX_VERTEX_NUM];
createDN(MGraph *G)
{ int IncInfo,i,j,k,v1,v2,w;
printf("Please input vertex number and arc number divided with space\n");
scanf("%d%d",&(G->vexnum),&(G->arcnum));
IncInfo=0;
for(i=0;i<G->vexnum;i++) G->vexs[i]=i+1;
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{(G->arcs[i][j]).adj=INF;
(G->arcs[i][j]).info=NULL;
}
printf("Please input two vertexes and its weight\n");
printf("from to weight\n");
for(k=0;k<G->arcnum;k++)
{
scanf("%d%d%d", &v1,&v2,&w);
i=locateVex(G,v1);j=locateVex(G,v2);
G->arcs[i][j].adj=w;
if(IncInfo)input(&G->arcs[i][j].info);
}
return 1;
}
locateVex(MGraph *G,int v)
{return v-1;
}
input(char **p)
{
}
main()
{MGraph G;
PathMatrix P;ShortPathTable D;
int u;
for(;;)
{
printf("1.create\n2.DIJ\n0.exit\n");
scanf("%d",&u);
switch(u)
{case 1:createDN(&G);break;
case 2:ShortestPath_DIJ(G,u,P,D);//问题就在这里,为什么把u改为1就能得到结果,而接收得到1就不能得到正确结果呢???
break;
case 0:exit(0);}
getch();}
}
ShortestPath_DIJ(MGraph G,int v0,PathMatrix P,ShortPathTable D)
{ int final[MAX_VERTEX_NUM];
int i,v,w,min,j;
v0=locateVex(&G,v0);
for(v=0;v<G.vexnum;v++)
{final[v]=false; D[v]=G.arcs[v0][v].adj;
for(w=0;w<G.vexnum;w++)P[v][w]=false;
if(D[v]<INF)
{P[v][v0]=true;P[v][v]=true;
}
}
D[v0]=0; final[v0]=true;
for(i=1;i<G.vexnum;i++)
{min=INF;
for(w=0;w<G.vexnum;w++)
if(!(final[w]))
if(D[w]<min){v=w;min=D[w];}
final[v]=true;
for(w=0;w<G.vexnum;w++)
if(!final[w] && (min+G.arcs[v][w].adj<D[w]))
{D[w]=min+G.arcs[v][w].adj;
for(j=0;j<G.vexnum;j++)P[w][j]=P[v][j];
P[w][w]=true;
}
}printf("The shortest path \n");
for(i=0;i<G.vexnum;i++)
{ printf("to %d <",i+1);
for(j=0;j<G.vexnum;j++)
if(P[i][j]==true)printf("%5d",j+1);
printf("> cost:\n",D[i]);
}
}
请大虾们出手相教~~~
#define INF 1000
#define MAX_VERTEX_NUM 20
#define true 1
#define false 0
typedef enum{DG,DN,UDG,UDN}GraphKind;
typedef int VRType;
typedef int VertexType;
typedef char InfoType;
typedef struct ArcCell{
VRType adj;
InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
typedef int PathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef int ShortPathTable[MAX_VERTEX_NUM];
createDN(MGraph *G)
{ int IncInfo,i,j,k,v1,v2,w;
printf("Please input vertex number and arc number divided with space\n");
scanf("%d%d",&(G->vexnum),&(G->arcnum));
IncInfo=0;
for(i=0;i<G->vexnum;i++) G->vexs[i]=i+1;
for(i=0;i<G->vexnum;i++)
for(j=0;j<G->vexnum;j++)
{(G->arcs[i][j]).adj=INF;
(G->arcs[i][j]).info=NULL;
}
printf("Please input two vertexes and its weight\n");
printf("from to weight\n");
for(k=0;k<G->arcnum;k++)
{
scanf("%d%d%d", &v1,&v2,&w);
i=locateVex(G,v1);j=locateVex(G,v2);
G->arcs[i][j].adj=w;
if(IncInfo)input(&G->arcs[i][j].info);
}
return 1;
}
locateVex(MGraph *G,int v)
{return v-1;
}
input(char **p)
{
}
main()
{MGraph G;
PathMatrix P;ShortPathTable D;
int u;
for(;;)
{
printf("1.create\n2.DIJ\n0.exit\n");
scanf("%d",&u);
switch(u)
{case 1:createDN(&G);break;
case 2:ShortestPath_DIJ(G,u,P,D);//问题就在这里,为什么把u改为1就能得到结果,而接收得到1就不能得到正确结果呢???
break;
case 0:exit(0);}
getch();}
}
ShortestPath_DIJ(MGraph G,int v0,PathMatrix P,ShortPathTable D)
{ int final[MAX_VERTEX_NUM];
int i,v,w,min,j;
v0=locateVex(&G,v0);
for(v=0;v<G.vexnum;v++)
{final[v]=false; D[v]=G.arcs[v0][v].adj;
for(w=0;w<G.vexnum;w++)P[v][w]=false;
if(D[v]<INF)
{P[v][v0]=true;P[v][v]=true;
}
}
D[v0]=0; final[v0]=true;
for(i=1;i<G.vexnum;i++)
{min=INF;
for(w=0;w<G.vexnum;w++)
if(!(final[w]))
if(D[w]<min){v=w;min=D[w];}
final[v]=true;
for(w=0;w<G.vexnum;w++)
if(!final[w] && (min+G.arcs[v][w].adj<D[w]))
{D[w]=min+G.arcs[v][w].adj;
for(j=0;j<G.vexnum;j++)P[w][j]=P[v][j];
P[w][w]=true;
}
}printf("The shortest path \n");
for(i=0;i<G.vexnum;i++)
{ printf("to %d <",i+1);
for(j=0;j<G.vexnum;j++)
if(P[i][j]==true)printf("%5d",j+1);
printf("> cost:\n",D[i]);
}
}
请大虾们出手相教~~~