07-Figure 4 The Harry Potter exam (Shortest path)

Source: Internet
Author: User

Purpose: The most difficult to change the kind of animals need the magic spell shortest.

Using the adjacency matrix, the shortest distance between each vertex is recorded by the Floyd algorithm.

With Weighttype findmaxdist (Weighttype d[][maxvertexnum], Vertex i,int N); Find the most difficult enchantments for the vertex to other vertices

Choose one of the shortest in the most difficult spells. That is, the most difficult to change the animal needs to the shortest spell. Output the animal

Harry Potter is going to take the exam and he needs your help. This course is about the ability to turn an animal into another animal with a spell. For example, the curse of turning a cat into a mouse is Haha, and the curse of turning a mouse into a fish is hehe and so on. The reverse direction of the curse is simply the original spell upside down, such as ahah can turn the mouse into a cat. In addition, if you want to turn the cat into a fish, you can read a direct spell Lalala, you can also change the cat into a mouse, the curse of the mouse into the fish to read: Hahahehe.

Now there is a textbook in Harry Potter's hand that lists all the Transfiguration spells and the animals that can change. The teacher allowed him to take an animal to the examination room by himself, to examine his ability to turn the animal into any designated animal. So he asks you: What kind of animal can you use to make the most difficult animal (that is, the most magical spell the animal needs to bring to Harry Potter) to the shortest possible spell? For example: If only cats, rats, and fish, it is obvious that Harry Potter should take the mouse, because the mouse into the other two animals only need to read 4 characters, and if you take a cat, you need to read at least 6 characters to turn the cat into a fish, the same is not the best choice.

Input format:

Enter a description: Enter the 1th line to give two positive integersNN\le 100≤100) andMM, whereNn is the total number of animals involved in the examination,MM is the number of enchantment bars used for direct deformation. For the sake of simplicity, we numbered the animals by 1~nN. Then m-m, each line gives 3 positive integers, respectively, the number of the two animals, and the length of the enchantments required to deform between them (\le≤100), separated by a space between the numbers.

Output format:

Output the number of animals that Harry Potter should take to the examination room, and the length of the longest distortion spell, separated by a space. If it is impossible to complete all variants with only 1 animals, then output 0. If there are several animals that can be alternate, the output number is the smallest one.

Input Sample:
6 113 4 701 2 15 4 502 6 505 6 601 3 704 6 603 6 805 1 1002 4 605 2 80
Sample output:
4 70


#include <iostream>#include<cstdio>#include<cstdlib>using namespacestd;#defineMaxvertexnum 100/* Maximum vertex count is set to 100 */#defineINFINITY 65535/* Sets the maximum value of a double-byte unsigned positive number 65535*/typedefintVertex;/*Vertex is represented by vertex subscript, integral type*/typedefintWeighttype;/*the weight of the edge is set to integer type*//*Definition of Edge*/typedefstructEnode *Ptrtoenode;structenode{Vertex V1, V2; /*<v1 to the side, v2>*/Weighttype Weight; /*Weights*/};typedef Ptrtoenode Edge; /*Definition of graph node*/typedefstructGnode *Ptrtognode;structgnode{intNv;/*number of vertices*/    intNe;/*Number of sides*/Weighttype G[maxvertexnum][maxvertexnum];/*adjacency Matrix*/};typedef Ptrtognode mgraph; /*graph types stored in adjacency matrices*/mgraph Creategraph (intvertexnum);voidInsertedge (mgraph Graph, Edge E); Mgraph buildgraph ();voidFloyd (mgraph Graph, Weighttype D[][maxvertexnum]); Weighttype findmaxdist (Weighttype d[][maxvertexnum], Vertex I,intN); voidfindanimal (Mgraph Graph);intMain () {mgraph graph; Graph=buildgraph ();    Findanimal (graph); return 0;} Mgraph Creategraph (intvertexnum) { /*Initializes a graph with a vertexnum vertex but no edges*/Vertex V, W;          Mgraph Graph; Graph= (mgraph) malloc (sizeof(structGnode));/*Build Diagram*/Graph-&GT;NV =Vertexnum; Graph->ne =0; /*initializing adjacency matrices*/    /*Note: Here The default vertex number starts at 0, to (Graph->nv-1)*/     for(v=0; v<graph->nv; v++)         for(w=0; w<graph->nv; w++) Graph-&GT;G[V][W] =INFINITY; returnGraph;}voidInsertedge (mgraph Graph, Edge E) {/*Insert Edge <v1, v2>*/Graph-&GT;G[E-&GT;V1][E-&GT;V2] = e->Weight; /*If there is no map, insert the Edge <v2, v1>*/Graph-&GT;G[E-&GT;V2][E-&GT;V1] = e->Weight;}    Mgraph buildgraph () {mgraph Graph;    Edge E; intNv, I; scanf ("%d", &AMP;NV);/*number of vertices read in*/Graph= Creategraph (NV);/*Initializes a graph with an NV vertex but no edges*/scanf ("%d", & (Graph->ne));/*number of read-in edges*/    if(Graph->ne! =0) {/*If there is an edge*/E= (Edge) malloc (sizeof(structEnode));/*set up edge nodes.*/         /*read-in Edge, format "Start-end weight", insert adjacency matrix*/         for(i=0; i<graph->ne; i++) {scanf (" %d%d%d", &e->v1, &e->v2, &E->Weight); E->v1--;//numbering starting from 0e->v2--; /*Note: If the weight is not integer, the read-in format of the weight should be changed*/Insertedge (Graph, E); }    }       returnGraph;}/*adjacency Matrix Storage-multi-source Shortest Path algorithm*/ voidFloyd (mgraph Graph, Weighttype D[][maxvertexnum]) {Vertex I, j, K; /*Initialize*/     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]; }}voidfindanimal (mgraph Graph) {weighttype d[maxvertexnum][maxvertexnum], MaxDist, mindist;        Vertex Animal;        Floyd (Graph, D); Mindist=INFINITY;  for(Vertex i =0; I < graph->nv; i++) {MaxDist= Findmaxdist (D, I, graph->Nv); if(MaxDist = = INFINITY) {//indicating that there are animals that cannot be changed from Iprintf"0\n"); return; }        if(Mindist > MaxDist) {//find the longest and smaller animalsMindist = MaxDist;//Update DistanceAnimal = i +1;//Record number}} printf ("%d%d\n", Animal, mindist); }weighttype findmaxdist (Weighttype d[][maxvertexnum], Vertex I,intN)    {Weighttype MaxDist; MaxDist=0;  for(Vertex j =0; J < N; J + +)//find the longest distance I to other animal J        if(I! = J && D[i][j] >MaxDist) MaxDist=D[i][j]; returnMaxDist;}

07-Figure 4 The Harry Potter exam (Shortest path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.