07 - Figure 4 Harry Potter's test (25 points)

07 - Figure 4 Harry Potter's test (25 points) Harry Potter is going to have an exam. He needs your help. This course is about the ability to chan...

07 - Figure 4 Harry Potter's test (25 points)

Harry Potter is going to have an exam. He needs your help. This course is about the ability to change one animal into another with a magic spell. For example, the spell to turn a cat into a mouse is haha, the spell to turn a mouse into a fish is hehe, and so on. A spell that changes in the opposite direction is simply to recite the original spell upside down. For example, ahah can turn a mouse into a cat. In addition, if you want to turn a cat into a fish, you can read a direct magic spell lalala, or you can connect the magic spells of cat into mouse and mouse into fish: hahahehe.

Now Harry Potter has a textbook in his hand, which lists all the deformation spells and animals that can change. The teacher allowed him to take an animal to the examination room to examine his ability to turn the animal into any designated animal. So he asked you: what animal can you take to make the most difficult animal (that is, the animal needs the longest spell to become the animal brought by Harry Potter himself) need the shortest spell? For example, if there are only cats, rats and fish, it is obvious that Harry Potter should take rats, because rats only need to read four characters to become two other animals; If you take a cat, you need to read at least 6 characters to turn the cat into a fish; Similarly, taking fish is not the best choice.

Input format:

Input Description: enter the first line to give two positive integers N   (≤ 100) and m, where N is the total number of animals involved in the test, and M is the number of magic spells used for direct deformation. For simplicity, we number the animals by 1~N. Then, in line m, each line gives three positive integers, which are the numbers of the two animals and the length of the spell required for deformation between them (≤ 100), and the numbers are separated by spaces.

Output format:

Output the number of the animal Harry Potter should take to the examination room and the length of the longest deformation spell, separated by a space. If it is impossible to complete all deformation requirements with only 1 animal, output 0. If several animals can be selected, the one with the lowest number will be output.

Input example:

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

No blank lines at the end

Output example:

4 70

No blank lines at the end

#include <stdio.h> #include <stdlib.h> #define MaxVertexNum 100 #define INFINITY 65535 typedef int Vertex;//The vertex is represented by the vertex subscript, which is an integer typedef int WeightType;//The weight of the edge is set to integer typedef char DataType;//You can delete it //Definition of edges typedef struct ENode *PtrToENode; struct ENode { Vertex V1,V2;//Directed edge < 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;//Number of sides WeightType G[MaxVertexNum][MaxVertexNum];//Collar matrix DataType Data[MaxVertexNum];//Save vertex data //Note: in many cases, the vertex has no Data, and Data [] can not appear at this time }; typedef PtrToGNode MGraph;//Graph type stored in collar matrix void FindAnimal(MGraph Graph);//Select animals MGraph CreateGraph(int VertexNum); void InsertEdge(MGraph Graph,Edge E); MGraph BuildGraph(); void Floyd(MGraph Graph,WeightType D[][MaxVertexNum]); WeightType FindMaxDist(WeightType D[][MaxVertexNum],Vertex i,int N); void FindAnimal(MGraph Graph) { WeightType D[MaxVertexNum][MaxVertexNum],MaxDist,MinDist; Vertex Animal,i; Floyd(Graph,D); MinDist=INFINITY; for(i=0;i<Graph->Nv;i++) { MaxDist=FindMaxDist(D,i,Graph->Nv); if(MaxDist==INFINITY) //It means there are animals that can't be changed from i { printf("0\n"); return; } if(MinDist>MaxDist) //Find the longest and smaller animal { MinDist=MaxDist; Animal=i+1; //Update distance, record number } } printf("%d %d\n",Animal,MinDist); } MGraph CreateGraph(int VertexNum) {//Initializes a graph with VertexNum vertices but no edges Vertex V,W; MGraph Graph; Graph=(MGraph)malloc(sizeof(struct GNode));//Build map Graph->Nv=VertexNum; Graph->Ne=0; //Initialize collar matrix //Note: the default vertex number here starts from 0 and ends at (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) { //Insert edge < V1, V2 > Graph->G[E->V1][E->V2]=E->Weight; //For an undirected graph, insert edges < V1, V2 > Graph->G[E->V2][E->V1]=E->Weight; } MGraph BuildGraph() { MGraph Graph; Edge E; Vertex V; int Nv,i; scanf("%d",&Nv);//Number of read vertices Graph=CreateGraph(Nv);//Initializes a graph with Nv vertices but no edges scanf("%d",&(Graph->Ne));//Number of read edges if(Graph->Ne!=0) //If there are edges { E=(Edge)malloc(sizeof(struct ENode));//Establish edge node //Read in the edge in the format of "start point and end point weight", and insert the collar 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 read format of weight should be changed E->V1--;E->V2--;//Starting from 0 InsertEdge(Graph,E); } } //If the vertex has data, read in the data for(V=0;V<Graph->Nv;V++) { scanf(" %c",&(Graph->Data[V])); } 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]; //If (I = = J & & D [i] [J] < 0) if a negative circle is found //return false; if the problem cannot be solved correctly, an error flag is returned } //return true; after the algorithm is executed, the correct flag is returned } WeightType FindMaxDist(WeightType D[][MaxVertexNum],Vertex i,int N) { WeightType MaxDist; Vertex j; MaxDist=0; for(j=0;j<N;j++)//Find the longest distance from i to other animals j if(i!=j&&D[i][j]>MaxDist) MaxDist=D[i][j]; return MaxDist; } int main() { MGraph G = BuildGraph(); FindAnimal(G); return 0; }

28 November 2021, 10:06 | Views: 6000

Add new comment

For adding a comment, please log in
or create account

0 comments