Minimum spanning tree premium Manaus algorithm

Source: Internet
Author: User
Tags strcmp

#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-&GT;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-&GT;U[J][K] = weight;
G-&GT;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 (&AMP;G);
Minispantree_prim (&AMP;G);
System ("pause");
return 0;
}

/*
A
S
3
A
D
6
S
D
8

*/

Minimum spanning tree premium Manaus algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.