Ii. Code and Analysis
# Include <iostream>
Using namespace STD;
Const int maxsize = 20;
Template <class T>
Class mgraph
{
Public:
Mgraph (t a [], int N, int e );
PRIVATE:
T vertex [maxsize];
Int arc [maxsize] [maxsize];
Int vertexnum, arcnum;
Friend void Floyd (mgraph <t> G, int V );
};
Template <class T>
Mgraph <t>: mgraph (t a [], int N, int e)
{
Vertexnum = N; arcnum = E; int I, J, K;
Cout <"vertex 0: school gate \ t vertex 1: Aquatic museum \ t vertex 2: Main Building" <Endl
<"Vertex 3: Library \ t vertex 4: gymnasium \ t vertex 5: Eastern stadium" <Endl
<": Vertex 6: West Lake \ t vertex 7: Small houshan \ t vertex 8: School Hospital" <Endl;
For (I = 0; I <vertexnum; I ++)
Vertex [I] = A [I]; // Vertex
For (I = 0; I <vertexnum; I ++)
For (Int J = 0; j <arcnum; j ++)
Arc [I] [J] = 0; // distance between initialized vertices
For (k = 0; k <arcnum; k ++)
{
Cout <"input vertex number :";
Cin> I> J;
For (;;)
{
If (I> vertexnum | j> vertexnum | I = J) {cout <"the input is invalid. Please re-enter:"; CIN> I >> J ;}
Else break;
}
Cout <"Input Point" <I <"to" <j <"Time (min ):";
Cin> arc [I] [J];
}
For (I = 0; I <vertexnum; I ++)
For (j = 0; j <arcnum; j ++)
{
If (I = J) arc [I] [J] = 0;
If (I! = J & arc [I] [J] = 0) arc [I] [J] = 99; // replace infinity with 99
Else
Arc [J] [I] = arc [I] [J];
}
}
Template <class T>
Void Floyd (mgraph <t> G, int v) // defines the user meta function to access a private member.
{
Int Dist [10] [20], path [10] [20]; int I, J, K;
For (I = 0; I <G. vertexnum; I ++)
For (j = 0; j <G. vertexnum; j ++)
{
Dist [I] [J] = G. Arc [I] [J]; // initialize Dist [I] [J]
If (Dist [I] [J]! = 99) path [I] [J] = 10 * G. vertex [I] + G. vertex [J]; // initialization path
}
For (k = 0; k <G. vertexnum; k ++)
For (I = 0; I <G. vertexnum; I ++)
For (j = 0; j <G. vertexnum; j ++)
If (Dist [I] [k] + dist [k] [J] <Dist [I] [J])
{
Dist [I] [J] = DIST [I] [k] + dist [k] [J];
Path [I] [J] = J * 100 + 10 * k + I; // number indicates the vertex of the shortest path
}
// Obtain the Shortest Path
Cout <"vertex 0: school gate \ t vertex 1: Aquatic museum \ t vertex 2: Main Building" <Endl
<"Vertex 3: Library \ t vertex 4: gymnasium \ t vertex 5: Eastern stadium" <Endl
<": Vertex 6: West Lake \ t vertex 7: Small houshan \ t vertex 8: School Hospital" <Endl;
For (;;)
{
Cout <"Enter the vertex to be queried, and enter 88 to exit:"; CIN> K;
For (I = 0; I <G. vertexnum; I ++) // query the vertex Information
If (k> G. vertexnum)
{
If (K! = 88) {cout <"the input is invalid. Please try again. "<Endl; break;} // enter a number other than the vertex number to re-Input
Else exit (0); // enter 88 to exit the program.
}
Else if (I! = K)
{
The shortest distance between cout <k <"and" <I <is "<Dist [k] [I] <Endl;
If (path [k] [I] & gt; 100)
Cout <"Shortest Path:" <k <"→" <(path [k] [I] % 100) /10 <"→" <I <Endl;
Else
Cout <"Shortest Path:" <k <"→" <I <Endl;
} // Output Shortest Path
}
}
Int main ()
{
Int A [9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; // initialize the number of vertices, which can be reduced as you like
Mgraph <int> mgraph (A, 8, 15 );
Floyd (mgraph, 0); // call the membership function
Return 0;
}
Iii. Gains, experiences, and shortcomings: As I found that the previous question 0 was referenced by many people, I even saw that other people's blog posts are exactly the same as what I posted before, so I sent another article, which was slightly changed and improved, such as the external DEFINITION OF THE youyuan function and a new selection point. At the same time, I feel that this teaching method is not necessary. If I want to learn it, I will still submit my homework on time. If I don't want to learn it, I still copy other blogs. This is no different from submitting my homework, you may not even know who copied it. I often give a rational explanation of various aspects: I have deepened my application and understanding of the program, and I have also reviewed the use of the functions for myself, however, it is not very powerful to let yourself know the programming ability. reading a book is one thing, and making your own edits is another thing. You must combine the books and make multiple edits at ordinary times to substantially improve your programming ability. This course design also reflects the principle of "getting to the bottom of the paper and learning how to do this. I am quite satisfied with the design of this course, because I do not usually do programming, but this time it was completed independently. Although it is not perfect, it is also my work. Although there are not many words, it is enough to explain the design feelings and gains.
Question 0 (2) If you want to refer to question 0, please respect yourself