Home > Back-end >  Can you help me with a convenient function to add or delete sites and line? Thank you very much
Can you help me with a convenient function to add or delete sites and line? Thank you very much

Time:10-28

#include
#include
#include
# define MaxVertexNum 25
# define INF 32767
Typedef int EdgeType;
Typedef struct
{
Char citys [MaxVertexNum] [10];
EdgeType edges [MaxVertexNum] [MaxVertexNum];
Int n, e;

} MGraph;
Int CityID (MGraph * G, char CityName [])
{
int i;
for(i=0; i{
If (STRCMP (CityName, G - & gt; Citys [I])==0)
break;
}
If (I==G - & gt; N)
return -1;
The else
return i;
}
Void CreatMGraph (MGraph * G)
{
Int I, j, k, l;
Char t [10], [10] m.;
Printf (" \ n information, please enter the subway exit: input q \ n ");
For (I=0;; I++)
{
Printf (" \ n the subway site name % d: ", I);
The scanf (" % s ", t);
If (STRCMP (t, "q")==0)
break;
If (CityID (G, t) & gt;=0)
{
Printf (" \ n for this site! \n");
I -;
continue;
}
Strcpy (G - & gt; The citys [I], t);
G - & gt; N=I + 1;
}
Printf (" \ n input fields of railway information, please enter q exit: \ n ");
For (I=0;; I++)
{

Printf (" \ n starting site: ");
The scanf (" % s ", t);
If (STRCMP (t, "q")==0)
break;
Printf (" termination site: ");
The scanf (" % s ", m);
If (STRCMP (m, "q")==0)
break;
Printf (" site distance: ");
The scanf (" % d ", & amp; L);
If (STRCMP (t, "q")==0)
break;
J=CityID (G, t);
K=CityID (G, m);
If (1==j | | - 1==k)
{
Printf (" \ n the site name input is wrong! \n");
continue;
}
G - & gt; Edges [j] [k]=1;
G - & gt; Edges [k] [j]=1;
}
G - & gt; E=I;
}
Typedef struct
{
Int adjvex;
Int lowcost;
} Closedge;
Void Prim (MGraph * G, int v)
{
Int k, I, j, minCost;
Closedge Closedge [MaxVertexNum];
Closedge [v] lowcost=0;
for(j=0; j{
Closedge [j] adjvex=v;
Closedge [j]. Journal of lowcost=G - & gt; Edges [v] [j];
}
for(i=1; i{
for(j=0; jIf (closedge [j]. Journal of lowcost!=0)
{
K=j;
break;
}
MinCost=closedge [k]. Lowcost;
for(j=0; jIf (closedge [j] lowcost{
MinCost=closedge [j]. Journal of lowcost;
K=j;
}
Printf (" () % s, % s \ n ", the G & gt; The citys [closedge [k]. Adjvex],
G - & gt; Citys [k]);
Closedge [k]. Lowcost=0;
for(j=0; j{
If (G - & gt; Edges [k] [j] {
Closedge [j] adjvex=k;
Closedge [j]. Journal of lowcost=G - & gt; Edges [k] [j];
}
}
getchar();
}
}
Void dispath (MGraph * G, int dist [], int the path [], [], int s int v)
{
Int I, k;
for(i=0; i[I] if (s==1)
{k=I;
Printf (" \ n the shortest path is: % s to % s ", the G & gt; The citys [v], G - & gt; Citys [I]);
While (k!=v)
{printf (" % s<- "G - & gt; Citys [k]);
K=path [k].
}
Printf (" % s need to pass the number of sites for: % d \ n ", the G & gt; Citys [v], dist [I]);
}
The else
Printf (" % s}
Void dijkstra (MGraph * G, int v)
{
Int dist [MaxVertexNum], the path [MaxVertexNum];
Int s [MaxVertexNum];
Int mindis;
Int I, j, k;
for(i=0; i{
Dist [I]=G - & gt; Edges [v] [I];
S [I]=0;
If (G - & gt; Edges [v] [I] <32767)
The path [I]=v;
The else
The path [I]=1;
}
S [v]=1;
for(i=0; i{
Mindis=INF;
K=v.
for(j=0; j{
If (s==0 [j] & amp; & Dist [j] {
K=j;
Mindis=dist [j];
}
}
S [k]=1;
for(j=0; j{
If (s==0 [j] & amp; & G - & gt; Edges [k] [j] <32767 & amp; & Dist [k] + G - & gt; Edges [k] [j] {
Dist [j]=dist [k] + G - & gt; Edges [k] [j];
The path [j]=k;
}
}
}
Dispath (G, dist, path, s, v);
}
Void consult (MGraph * G)
{
Char c [10];
int i;
Printf (" \ n please input on the site: ");
The scanf (" % s ", c);
I=CityID (G, c);
If (1==I)
{
Printf (" \ n can't find this site!" );
getchar();
return;
}
Dijkstra (G, I);
getchar();
}
Int menu ()
{
int n;
char c;
Printf (" \ n \ n \ t \ t urban subway traffic system: \ n ");
Printf (" \ n \ t \ \ t t1 to establish urban subway traffic map ");
Printf (" \ n \ t \ \ t t2 all the site's minimum spanning tree ");
Printf (" \ n \ t \ \ t t3 subway site consulting ");
Printf (" \ n \ t \ \ t t0 exit ");
Do
{
fflush(stdin);
Printf (" \ n \ n \ t \ t enter 0 ~ 3 option: ");
C=getchar ();
N=c - 48;
If (n<0 | | n> 3)
Printf (" \ t \ \ t t input error option! Please input again option ");
} while (n<0 | | n> 3);
return n;
}
Int main ()
{
Int the select, I, j;
Char c [10];
MGraph * G;
G=(MGraph *) malloc (sizeof (MGraph));
G - & gt; N=0;
G - & gt; E=0;
for(i=0; ifor(j=0; jIf (I.=j)
G - & gt; Edges [I] [j]=INF;
The else

G - & gt; Edges [I] [j]=0;
Do
{
System (" CLS ");
The select=menu ();
The switch (select)
{
Case 1: CreatMGraph (G); break;
Case 2: Prim (G, 0); break;
Case 3: consult (G); break;
Case 0: printf (" \ n \ n \ n \ t \ \ t t thank you! Bye,,, \ n ");
getchar();
exit(0);

}
getchar();
} while (select!=0);
}





  • Related