图的邻接矩阵建立法的疑问
看到书上一段程序,但是不太明白他那句“输入边(vi,vj)上的下标i,下标j和权值:\n”是要输入什么。。。能不能请各位大神看看,给我个输入样例...程序如下:
#include <cstdio>
#include <cstring>
#include <cstdlib>
typedef char VertexType;
typedef int EdgeType;
#define MAXVEX 100
#define INF 0
typedef struct
{
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}Graph;
//定位
int locates(Graph *g, char ch)
{
int i = 0;
for (i = 0; i < g->numVertexes; i++)
{
if (g->vexs[i] == ch)
{
return i;
}
}
if (i >= g->numVertexes)
{
return -1;
}
}
//建立一个无向网图的邻接矩阵表示
void CreateGraph(Graph *g)
{
int i, j, k, w;
printf("输入顶点数和边数:\n");
scanf("%d %d", &(g->numVertexes), &(g->numEdges));
printf("%d %d\n", g->numVertexes, g->numEdges);
printf("输入顶点:\n");
for (i = 0; i < g->numVertexes; i++)
{
g->vexs[i] = getchar();
while (g->vexs[i] == '\n')
{
g->vexs[i] = getchar();
}
}
for (i = 0; i < g->numVertexes; i++)
{
printf("%c ", g->vexs[i]);
}
printf("\n");
for (i = 0; i < g->numVertexes; i++)
{
for (j = 0; j < g->numVertexes; j++)
{
g->arc[i][j] = INF; //邻接矩阵初始化
}
}
for (k = 0; k < g->numEdges; k++)
{
char p, q;
printf("输入边(vi,vj)上的下标i,下标j和权值:\n");
p = getchar();
while (p == '\n')
{
p = getchar();
}
q = getchar();
while (q == '\n')
{
q = getchar();
}
scanf("%d", &w);
int m = -1;
int n = -1;
m = locates(g, p);
n = locates(g, q);
if (n == -1 || m == -1)
{
fprintf(stderr, "there is no this vertex.\n");
return;
}
//getchar();
g->arc[m][n] = w;
g->arc[n][m] = g->arc[m][n];
}
}
void printGraph(Graph g)
{
int i, j;
for (i = 0; i < g.numVertexes; i++)
{
for (j = 0; j < g.numVertexes; j++)
{
printf("%6d ", g.arc[i][j]);
}
printf("\n");
}
}
int main()
{
Graph g;
CreateGraph(&g);
printGraph(g);
return 0;
}