Harry Potter's exam Java description

Source: Internet
Author: User

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 integersN≤) andm, where n is the total number of animals involved in the exam, andm is the number of enchantment bars used for direct deformation. For the sake of simplicity, we numbered the animals by 1~N. Then m, each row gives 3 positive integers, respectively, the number of the two animals, and the length of the spell needed to deform between them, separated by a space.

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
ImportJava.util.Scanner;classGraph {Scanner sc; intnum; intedges; int[] graph; //Initialize the diagram to initialize the diagonal of the graph to 0, and the other elements to initialize to 10000     PublicGraph () {SC=NewScanner (system.in);  This. num=Sc.nextint ();  This. edges=Sc.nextint (); Graph=New int[Num][num];  for(intI=0;i<num; i++ )        {             for(intJ=0;j<num; j + + )            {                if(i==j) Graph[i][j]=0; ElseGraph[i][j]=10000; }        }    }    //to create an adjacency graph from an input edge     Public int[] Buildgraph () { for(intI=1;i<=edges; i++ )        {                    intFront=sc.nextint ()-1; intEnd=sc.nextint ()-1; intweight=Sc.nextint (); Graph[front][end]=weight; Graph[end][front]=weight; }        returngraph; }    //using floyed algorithm to calculate the shortest distance of any two points     Public int[] floyed (int[] arr) {         for(intK=0;k<arr.length; k++ )        {             for(intI=0;i<arr.length; i++ )            {                 for(intJ=0;j<arr.length; j + + )                {                    if(arr[i][j]>arr[i][k]+Arr[k][j]) {Arr[i][j]=arr[i][k]+Arr[k][j]; Arr[j][i]=arr[i][k]+Arr[k][j]; }                }            }        }        returnarr; }    //find the largest element of each row in the graph and put in an array, and the array angle tag represents the animal number     Public int[] Findmax (int[] arr) {        int[] list=New int[Arr.length];  for(intI=0;i<arr.length; i++ )        {            intMax=0;  for(intJ=0;j<arr.length; j + + )            {                if(arr[i][j]>max) Max=Arr[i][j]; } List[i]=Max; }        returnlist; }    //find the smallest distance in the array     Public voidFindmin (int[] list) {        intmin=10000; intAnimal=0; intflag=0;//to determine whether any animal can be transformed into any other animal .         for(intI=0;i<list.length; i++ )        {            if(flag<List[i]) flag=List[i]; if(list[i]<min) {min=List[i]; Animal=i+1; }        }        if(flag==10000) System.out.print (0); ElseSystem.out.print (Animal+" "+min); }}classtest{ Public Static voidMain (string[] args) {Graph g=NewGraph (); int[] result=g.buildgraph ();result=g.floyed (Result); int[] arr=G.findmax (Result);    G.findmin (arr); }}

Print results

Harry Potter's exam Java description

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.