利用Dijkstra算法实现最短路径搜索 的程序中, 我怎么运行不出来呢? 电脑显示错误:f:\vc练习\lin.cpp(52) : error C2065: 'clrscr' : undeclared identifier f:\vc练习\lin.cpp(53) : error C2065: 'malloc' : undeclared identifier f:\vc练习\lin.cpp(68) : error C2065: 'getch' : undeclared identifier f:\vc练习\lin.cpp(68) : warning C4508: 'main' : function should return a value; 'void' return type assumed 执行 cl.exe 时出错.
lin.exe - 1 error(s), 0 warning(s)
[此贴子已经被作者于2005-10-12 17:31:01编辑过]
支持一下.
[CODE]#include "stdio.h"
#include "string.h"
#define MAXINT 0xfff
#define MAXSIZE 100
typedef struct
{
char name[20];
int value;
}infotype;
typedef struct ArcNode
{
int weight;
infotype *info;
}ArcNode;
typedef struct
{
char v[MAXSIZE];
ArcNode arc[MAXSIZE][MAXSIZE];
int vertexnum,arcnum;
}Graph;
Graph g;
int Locate(char ch)
{
int i;
for(i=0;g.v[i]!=ch;i++);
return i;
}
void Dijkstra(int posObt,int cost[])
{
int i,j,w,sum,temp;
bool set[MAXSIZE+1];
memset(set,false,sizeof(set));
for(i=0;i<g.vertexnum;i++)
{
cost[i]=g.arc[posObt][i].weight;
}
set[posObt]=true;
for(i=0;i<g.vertexnum;i++)
{
temp=MAXINT;
for(j=0;j<g.vertexnum;j++)
if(set[j]==false&&cost[j]<temp)
{
temp=cost[j];
w=j;
}
set[w]=true;
for (j=0;j<g.vertexnum;j++)
if (set[j]!=true)
{
sum=cost[w]+g.arc[w][j].weight;
if (sum<cost[j])
cost[j]=sum;
}
}
}
int main(void)
{
int i,j,pos_x,pos_y,posObt,wei;
int cost[MAXSIZE+1];
char start[2],end[2];
char search;
printf("Please input the number of the vertexs and the arcs:\n");
do{
scanf("%d%d",&g.vertexnum,&g.arcnum);
if(g.vertexnum>100)
printf("The number is too large.Please input a smaller number:\n");
}while(g.vertexnum>100);
for(i=0;i<g.vertexnum;i++)
for(j=0;j<g.vertexnum;j++)
{
g.arc[i][j].weight=MAXINT;
g.arc[i][j].info=NULL;
}
printf("Now input the vertex:\n");
fflush(stdin);
for(i=0;i<g.vertexnum;i++)
g.v[i]=getchar();
printf("Now input the arcs:(for example:a b 10)\n");
for(i=0;i<g.arcnum;i++)
{
scanf("%s%s%d",start,end,&wei);
pos_x=Locate(start[0]);
pos_y=Locate(end[0]);
g.arc[pos_x][pos_y].weight=wei;
}
fflush(stdin);
printf("Now input the vertex you want to search its minimal path to other vertexs:\n");
search=getchar();
posObt=Locate(search);
Dijkstra(posObt,cost);
for(i=0;i<g.vertexnum;i++)
if(i!=posObt)
{
printf("%c->%c:%d\n",g.v[posObt],g.v[i],cost[i]);
}
return 0;
}[/CODE]
floyd算法虽然算的是全源最短路径,但写起来就更简单了,就不详细写了,
只写个大概:
void Floyd(Fl[][],Edge[][],n)
{ for ( i = 1; i <= n; i++ )
for ( j = 1; j<=n; j++ )
Fl[i][j] = Edge[i][j] ;
for ( k = 1; k <= n; k++ )
for ( i = 1; i <= n; i++ )
for ( j = 1; j <=n; j++ )
if ( Fl[i][k] + Fl[k][j] < Fl[i][j] )
Fl[i][j] = Fl[i][k] + Fl[k][j] ;
}