1. 程式人生 > >7-26 Harry Potter's Exam(25 分)

7-26 Harry Potter's Exam(25 分)

ext quest numbers ini for each lan ng- should you

In Professor McGonagall‘s class of Transfiguration, Harry Potter is learning how to transform one object into another by some spells. He has learnt that, to turn a cat into a mouse one can say docamo! To reverse the effect, simply say decamo! Formally speaking, the transfiguration spell to transform between object A and object B is said to be S

if there are two spells, doS and deS, to turn A into B and vice versa, respectively.

In some cases, short-cut spells are defined to make transfiguration easier. For example, suppose that the spell to transform a cat to a mouse is docamo, and that to transform a mouse into a fatmouse is dofamo, then to turn a cat into a fatmouse one may say docamodofamo

! Or if a shot-cut spell is defined to be cafam, one may get the same effect by saying docafam!

Time is passing by quickly and the Final Exam is coming. By the end of the transfiguration exam, students will be requested to show Professor McGonagall several objects transformed from the initial objects they bring to the classroom. Each of them is allowed to bring 1 object only.

Now Harry is coming to you for help: he needs a program to select the object he must take to the exam, so that the maximum length of any spell he has to say will be minimized. For example, if cat, mouse, and fatmouse are the only three objects involved in the exam, then mouse is the one that Harry should take, since it will take a 6-letter spell to turn a mouse into either a cat or a fatmouse. Cat is not a good choice since it will take at least a 7-letter spell to turn it into a fatmouse. And for the same reason Harry must not take a fatmouse.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive integers N (100) and M, which are the total number of objects involved in the exam and the number of spells to be tested, respectively. For the sake of simplicity, the objects are numbered from 1 to N. Then M lines follow, each contains 3 integers, separated by a space: the numbers of two objects, and the length of the spell to transform between them.

Output Specification:

For each test case, print in one line the number of the object which Harry must take to the exam, and the maximum length of the spell he may have to say. The numbers must be separated by a space.

If it is impossible to complete all the transfigurations by taking one object only, simply output 0. If the solution is not unique, output the one with the smallest number.

Sample Input:

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

Sample Output:

4 70


好不容易讀懂題意,題意是說哈利波特做變形魔法,念出咒語可以可以直接變形,也可以間接變形,比如把貓變成老鼠,再把老鼠變成胖子,只要最終能把a變成b,那麽a與b就是可達的,題目給出n個對象,m個咒語,要求找出一個對象,這個對象可以完成到其他對象的轉變,然後找出轉變中咒語最長長度,如果存在多個滿足條件的對象,要求最小的那個。

代碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define inf 999999999
int n,m;
int e[101][101];
int main()
{
    int a,b,pri;
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= n;j ++)
        {
            e[i][j] = inf;
        }
    }
    for(int i = 0;i < m;i ++)
    {
        scanf("%d%d%d",&a,&b,&pri);
        e[a][b] = e[b][a] = pri;
    }
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= n;j ++)
        {
            for(int k = 1;k <= n;k ++)
            {
                if(e[j][i] + e[i][k] < e[j][k])e[j][k] = e[j][i] + e[i][k];
            }
        }
    }
    int min = inf,which = -1;
    for(int i = 1;i <= n;i ++)
    {
        int max = -1,j;
        for(j = 1;j <= n;j ++)
        {
            if(e[i][j] == inf)break;
            if(i != j&&e[i][j] > max)max = e[i][j];
        }
        if(j > n)
        {
            if(min > max)min = max,which = i;
        }
    }
    if(which == -1)printf("0");
    else printf("%d %d",which,min);
}

7-26 Harry Potter's Exam(25 分)