#define _crt_secure_no_warnings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
#define INFINITY 0x7fffffff
typedef struct
{
int U[max][max];
Char adj[max][10];
int Vexnum, arcnum;
}graph;
typedef struct
{
Char Temp_adj[max];
int temp_weight;
}closedge;
void creat (Graph *g)
{
int I, weight;
Char tail[10],head[10];
printf ("Please enter the number of vertices:");
scanf ("%d", &g->vexnum);
printf ("Please enter the number of arcs:");
scanf ("%d", &g->arcnum);
printf ("Please enter each vertex:");
Fflush (stdin);
for (i = 0; i < g->vexnum; i++)
for (int j = 0; J < g->vexnum; J + +)
{
G->U[I][J] = INFINITY;
}
for (i = 0; i < g->vexnum; i++)
scanf ("%s", G->adj[i]);
printf ("Please enter the figure:");
for (i = 0; i < g->arcnum; i++)
{
scanf ("%s%s%d", tail,head,&weight);
for (int j = 0; J < g->vexnum; J + +)
{
if (!strcmp (G->adj[j], tail))
for (int k = 0; k < g->vexnum; k++)
if (!strcmp (G->adj[k], head))
{
G->U[J][K] = weight;
G->U[K][J] = weight;
Break
}
}
}
/*for (i = 0; i < 3; i++)
for (int j = 0; J < 3; J + +)
printf ("%d\n", g->u[i][j]); * *
}
void Minispantree_prim (Graph *g)
{
Closedge Closedge[max];
int I, j, K,n=1;
int temp;
k = 0;
for (i = 1; i < g->vexnum; i++)
{
strcpy (Closedge[i].temp_adj, g->adj[k]);
Closedge[i].temp_weight = g->u[0][i];
}
closedge[0].temp_weight = 0;
for (i = 1; i < g->vexnum; i++)
{
temp = g->u[k][1];
for (j = 1; J < g->vexnum; J + +)
{
if (temp > G->u[k][j])
{
temp = g->u[k][j];
n = j;
}
}
printf ("%s---%s---%d\n", closedge[n].temp_adj,g->adj[n], closedge[n].temp_weight);
G->u[k][n] = INFINITY;
G->u[n][k] = INFINITY;
k = n;
for (j = 1; J < g->arcnum; J + +)
{
if (G->u[k][j] < closedge[j].temp_weight)
{
strcpy (Closedge[j].temp_adj, g->adj[k]);
Closedge[j].temp_weight = g->u[k][j];
}
}
}
}
int main ()
{
Graph G;
Creat (&G);
Minispantree_prim (&G);
System ("pause");
return 0;
}
/*
A
S
3
A
D
6
S
D
8
*/
Minimum spanning tree premium Manaus algorithm