Home > Back-end >  Park tourist map: design a tourist map of the park
Park tourist map: design a tourist map of the park

Time:09-25

Park tourist map: design a park tourist map, tourists by terminal: whether from one scenic spot to another attraction of the shortest path, visitors enter, from the entrance to the park to choose a best route, visitors can not repeatedly visit the scenic spots, finally return to exports (exports just beside the entrance),
Need very detailed process
Especially realize the visitors from the entrance to the park to enter, choose a best route, visitors can not repeatedly visit the scenic spots, finally return to exports (exports just beside the entrance),
Change the following code,
# include
# include
# include
100//# define MVNum maximum vertex number
# define MAXEDGE 10000
# define datatype int
# define Maxint 32767
# define MAX 100
Enum Boolean {FALSE, TRUE};
Int D1 [MVNum], p1 [MVNum];
Int D [MVNum] [MVNum], [MVNum] [MVNum] p;
Typedef struct
{
Int the begin;
Int the end;
Int weight;
} edge;
Typedef struct
{
Int adj.
Int weight;
} AdjMatrix [MAX] [MAX];
Typedef struct
{
AdjMatrix arc;
Int vexnum arcnum;
} MGraph;
Void CreatGraph (MGraph *, int n, int m);//function declaration
Void sort (edge *, MGraph *, int m);
Void MiniSpanTree (MGraph *, int n, int m);
Int the Find (int x, int);
Void Swapn (edge *, int, int).
Void Dijkstra (MGraph * G, int v1, int n);
Void Floyd (MGraph * G, int n);
Void the save (MGraph * G, int n1, int m1)
{
The FILE * fp.
Fp=fopen (" sheji. TXT ", "a +");
Fprintf (fp, "spots (% d, % d), and the distance between them is % 4 d \ n", n1, m1, G - & gt; Arc (n1) [r]. M1 weight);
The fclose (fp);
}
Void CreatGraph MGraph * G, int n, int (m)//component figure
{
Int I, j, n1, m1;
for(i=1; i<=n; I++)
{
for(j=1; j<=n; J++)
{
G - & gt; The arc [I] [j]. Journal of adj=G - & gt; The arc [j] [I] adj=0;
G - & gt; The arc [I] [j]. Journal of weight=G - & gt; The arc [j] [I]. Weight=Maxint;
}
}
for( i=1; i<=m; I++)//input side and weights
{
Printf (" \ n please input has two vertices: \ n ");
The scanf (" % d % d ", & amp; N1, & amp; M1);
While (n1 & lt; 0 | | m1 & lt; 0 | | n1 & gt; N | | m1 & gt; N)
{
Printf (" input the number does not conform to the requirements, please enter again: ");
The scanf (" % d % d ", & amp; N1, & amp; M1);
}
G - & gt; Arc (n1) [r]. M1 adj=G - & gt; Arc (m1) [n1] adj=1;
Printf (" \ n please weights between input % d to % d: ", n1, m1);
The scanf (" % d ", & amp; G - & gt; Arc (n1) [r]. M1 weight);
G - & gt; The arc (m1) [n1]. Weight=G - & gt; The arc (n1) (m1). The weight;
Save (G, n1, m1);
}
Printf (" adjacency matrix is: \ n ");
for(i=1; i<=n; I++)
{
for(j=1; j<=n; J++)
{
Printf (" % d ", the G & gt; Arc [I] [j]. Journal of adj);
}
printf("\n");
}
}
Void sort (edge edges [], MGraph * G, int m)//prioritize
weights{
Int I, j;
for(i=1; i{
For (j=I + 1; j<=m; J++)
{
If (edges [I] weight> Edges [j]. Journal of weight)
{
Swapn (edges, I, j);
}
}
}
Printf (" right after the sorting is: \ n ");
for(i=1; i{
Printf (" & lt; <% d, % d> % d \ n ", edges [I] the begin, edges [I] end, edges [I] weight);
}

}
Void Swapn (edge * edges, int, int j)//exchanging weights as well as the head and tail
{
int temp;
Temp=edges [I] the begin;
Edges [I] the begin=edges [j]. The begin;
Edges [j]. The begin=temp;
Temp=edges [I]. End;
Edges [I]. End=edges [j]. Journal of end;
Edges [j]. Journal of end=temp;
Temp=edges [I]. Weight;
Edges [I]. Weight=edges [j]. Journal of weight;
Edges [j]. Weight=temp;
}
Void MiniSpanTree (MGraph * G, int n, int m)//generates minimum spanning tree
{
Int I, j, n1, m1;
Int k=1;
Int the parent [MVNum];
Edge edges [MVNum];
for(i=1; i{
For (j=I + 1; j<=n; J++)
{
If (G - & gt; The arc [I] [j]. Journal of adj==1)
{
Edges [k]. Begin=I;
Edges [k]. End=j;
Edges [k]. Weight=G - & gt; The arc [I] [j]. Journal of weight;
K++;
}
}
}
Sort (edges, G, m);
for(i=1; i<=m; I++)
{
The parent [I]=0;
}
Printf (" minimum spanning tree is: \ n ");
for(i=1; i<=m; The core part i++)//
{
N1=Find (parent, edges [I] the begin);
M1=Find (parent, edges [I] end);
If (n1!=m1)
{
The parent (n1)=m1;
Printf (" & lt; <% d, % d> % d \ n ", edges [I] the begin, edges [I] end, edges [I] weight);
}
}
}

Int the Find (int * parent, int f)//Find the tail
{
While (the parent [f] & gt;=0)
{
F=parent [f];
}
Return (f);
}
Void Dijkstra (MGraph * G, int v1, int n)
{
//with the Dijkstra algorithm directed graph G v1 vertex to other v of the shortest path p [v] [v] and its right to D
//G is a directed graph adjacency matrix, if side & lt; I, j> Does not exist, the G [I] [j]=Maxint
//S [v] is true if and only if v S, which is obtained from v1 to v of the shortest path
Int [MVNum] D2, p2 [MVNum];
Int v, I, w, min;
Enum Boolean S [MVNum];
For (n=1; V<=n; V++)
{
//initialize the S and D
S [v]=FALSE;//empty the shortest path end set
D2 [v]=G - & gt; The arc [v1] [v]. Weight;//set the initial value of the shortest path
If (D2 [v] The p2 [v]=v1;//v1 is the trend of the v before
The else p2 [v]=0;//v than hasten
}//end_for
D2 (v1)=0; S [v1]=TRUE;//S set only when the initial source point and the source points to the source of the distance to 0
//cycle, each time, finding v1 vertices of the shortest path to a v and v to S concentration
For (I=2; i{//the remaining vertices n - 1
Min=Maxint;
For (w=1; W<=n; W++)
if(! S [w] & amp; & D2 [w] {
V=w; Min=D2 [w];
}//w vertex from v1 closer
S [v]=TRUE;
For (w=1; W<=n; W++)//update the current shortest path and distance
if(! S [w] & amp; & (D2 [v] + G - & gt; The arc [v] [w] weight{//modify D2 and p2 [w] [w], w belongs to V -s
D2=D2 [v] [w] + G - & gt; The arc [v] [w]. Weight;
The p2 [w]=v;
}//end_if
}//end_for
Printf (" path length path \ n ");
for(i=1; i<=n; I++)
{
Printf (" % 5 d, "[I] D2);
Printf (" % 5 d ", I);
V=p2 [I];
While (v!=0)
{
Printf (" & lt; - % d ", v);
V=p2 [v]; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related