1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <stdio.h>
#define INFINITY 65535 #define MAXVEX 100
typedef char VertexType; typedef int EdgeType;
typedef struct { VertexType vex[MAXVEX]; EdgeType edge[MAXVEX][MAXVEX]; int numVex, numEdge; } AMGraph;
void CreateAMGraph(AMGraph *G);
int main(void) { AMGraph G;
CreateAMGraph(&G);
printf("顶点数: %d,边数: %d\n", G.numVex, G.numEdge); printf("边(%c,%c)的权值: %d\n", G.vex[2], G.vex[0], G.edge[2][0]); printf("边(%c,%c)的权值: %d\n", G.vex[2], G.vex[3], G.edge[2][3]); printf("边(%c,%c)的权值: %d\n", G.vex[3], G.vex[1], G.edge[3][1]);
return 0; }
void CreateAMGraph(AMGraph *G) { int i, j, k, w;
printf("请输入顶点数和边数(以空格分隔):"); scanf("%d %d", &G->numVex, &G->numEdge); while ( getchar() != '\n');
printf("请输入顶点信息(连续输入,不用空格分隔):"); for ( i = 0; i < G->numVex; i++ ) { scanf("%c", &G->vex[i]); } while ( getchar() != '\n');
for ( i = 0; i < G->numVex; i++ ) { for ( int j = 0; j < G->numVex; j++ ) { G->edge[i][j] = INFINITY; } }
for ( k = 0; k < G->numEdge; k++ ) { printf("请输入边(Vi,Vj)的下标i、下标j和权值w(以空格分隔):"); scanf("%d %d %d", &i, &j, &w); G->edge[i][j] = w; G->edge[j][i] = w; } }
|