Topic 1:
07-Figure 4 Harry Potter's exam (25 points)
Harry Potter is going to have an exam. He needs your help. This course is about the ability to turn one animal into another by magic spells. For example, the magic spell of turning a cat into a mouse is haha, the magic spell of turning a mouse into a fish is hehe and so on. The magic spell of reversal is simply to recite the original magic spell, for example, ahahah can turn a mouse into a cat. In addition, if you want to change a cat into a fish, you can read a direct magic spell, lalala la, or you can read the magic spell of a cat into a mouse and a mouse into a fish together: hahahehe.
Now Harry Potter has a textbook that lists all the transformational spells and animals. The teacher allowed him to take an animal with him to the examination room. He wanted to examine his ability to turn the animal into any designated animal. So he came to ask you: What animal does it take to make the most difficult animal (that is, the animal that Harry Potter himself brings the longest spell) need the shortest spell? For example, if only cats, mice and fish, then obviously Harry Potter should take mice with him, because it takes only four characters for mice to become two other animals, and at least six characters for cats to become fish. Similarly, taking fish is not the best choice.
Input format:
Input description: Input line 1 gives two positive integers NN (\le) 100 < 100) and MM, where NN is the total number of animals involved in the exam, and MM is the number of magic bars used for direct deformation. For simplicity, we numbered the animals 1-N. Then MM lines, each line gives three positive integers, the number of two animals, and the length of the spell needed to deform between them (\le) Numbers are separated by spaces.
Output format:
The number of the animal that Harry Potter should take to the examination room and the length of the longest metamorphic spell should be separated by spaces. If it is impossible to complete all the deformation requirements with only one animal, output 0. If there are several animals to choose from, the one with the smallest number will be output.
Input sample:
6 11 3 4 70 1 2 1 5 4 50 2 6 50 5 6 60 1 3 70 4 6 60 3 6 80 5 1 100 2 4 60 5 2 80
2 Thoughts: Expressed by Adjacent Matrix and Folyd Algorithms
3 code:
/* Name: Harry Potter Examination Copyright: Author: demosses Date: 26/05/17 15:21 Description: It is expressed by adjacency matrix, and the minimum value between all nodes is obtained. Floyd algorithm is adopted. */ /* Adjacent Matrix Representation of Graphs */ #include <stdio.h> #include <string.h> #include <stdlib.h> #define MaxVertexNum 100/* Set the maximum number of vertices to 100*/ #define INFINITY 65535 /* _ Set to the maximum value of double-byte unsigned integers 65535*/ typedef int Vertex; /* Use vertex subscripts to represent vertices, which are integers */ typedef int WeightType; /* The weight of the edge is set to integer */ typedef char DataType; /* Vertex stored data type set to character type */ /* Definition of edge */ typedef struct ENode *PtrToENode; struct ENode{ Vertex V1, V2; /* Directed edges < V1, V2 > */ WeightType Weight; /* weight */ }; typedef PtrToENode Edge; /* Definition of Graph Nodes */ typedef struct GNode *PtrToGNode; struct GNode{ int Nv; /* Number of vertices */ int Ne; /* Edge number */ WeightType G[MaxVertexNum][MaxVertexNum]; /* adjacency matrix */ }; typedef PtrToGNode MGraph; /* Graph types stored in adjacency matrices */ MGraph CreateGraph( int VertexNum ) { /* Initialize a graph with VertexNum vertices but no edges */ Vertex V, W; MGraph Graph; Graph = (MGraph)malloc(sizeof(struct GNode)); /* Mapping */ Graph->Nv = VertexNum; Graph->Ne = 0; /* Initialization of adjacency matrix */ /* Note: The default vertex number here starts at 0 and goes to (Graph - > Nv - 1) */ for (V=0; V<Graph->Nv; V++) for (W=0; W<Graph->Nv; W++) Graph->G[V][W] = INFINITY; return Graph; } void InsertEdge( MGraph Graph, Edge E ) { /* Insertion Edge < V1, V2 > */ Graph->G[E->V1-1][E->V2-1] = E->Weight; Graph->G[E->V2-1][E->V1-1] = E->Weight; } MGraph BuildGraph() { MGraph Graph; Edge E; Vertex V; int Nv, i,j; scanf("%d", &Nv); /* Number of read vertices */ Graph = CreateGraph(Nv); /* Initialize graphs with Nv vertices but no edges */ scanf("%d", &(Graph->Ne)); /* Reading Edge Number */ if ( Graph->Ne != 0 ) { /* If there is a side */ E = (Edge)malloc(sizeof(struct ENode)); /* Establishing Edge Node */ /* Read-in edge, in the form of "Start and End Weight", insert adjacency matrix */ for (i=0; i<Graph->Ne; i++) { scanf("%d %d %d", &E->V1, &E->V2, &E->Weight); /* Note: If the Weight is not an integer, the Weight reading format should be changed. */ InsertEdge( Graph, E ); } } return Graph; } void Floyd( MGraph Graph, WeightType D[][MaxVertexNum] ) { Vertex i, j, k; /* Initialization */ for ( i=0; i<Graph->Nv; i++ ) for( j=0; j<Graph->Nv; j++ ) { D[i][j] = Graph->G[i][j]; } for( k=0; k<Graph->Nv; k++ ) for( i=0; i<Graph->Nv; i++ ) for( j=0; j<Graph->Nv; j++ ) if( D[i][k] + D[k][j] < D[i][j] ) { D[i][j] = D[i][k] + D[k][j]; } } int main() { int i,j,max[MaxVertexNum],min; MGraph Graph; Graph=BuildGraph(); WeightType D[MaxVertexNum][MaxVertexNum]={0}; Floyd(Graph,D); for(i=0;i<Graph->Nv;i++) D[i][i]=0; for(i=0;i<Graph->Nv;i++){ max[i]=D[i][0]; for(j=1;j<Graph->Nv;j++) { if(D[i][j]>max[i]) max[i]=D[i][j]; } } min=0; for(i=1;i<Graph->Nv;i++) if(max[min]>max[i]) min=i; if (max[min]<INFINITY) printf("%d %d",min+1,max[min]); else printf("%d",0); return 0; }