7-26 Harry Potter's Exam(25 point(s))(Floyd算法)

来源:互联网 发布:iphone翻墙用什么软件 编辑:程序博客网 时间:2024/06/13 06:38

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

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 saydocamo! To reverse the effect, simply say decamo! Formally speaking, the transfiguration spell to transform between object A and object B is said to beS 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 isdocamo, and that to transform a mouse into a fatmouse is dofamo, then to turn a cat into a fatmouse one may saydocamodofamo! Or if a shot-cut spell is defined to be cafam, one may get the same effect by sayingdocafam!

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 integersN (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 toN. 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 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思路:这道题其实是用弗洛伊德算法求每个点到其他各点的最短路径,然后选择出,以哪个点位起点时,它到各点的最短路径中最长的那一条,和以其他点为起点最长的那一条相比最短有以下几点注意!!:物品不能变自己,所以循环中如果到了自己变自己的跳过即可,其次,每个物品都能变成其他物品,如果以某个物品不能变成其中的一个物品,那么这个物品不符合,直接可以中断这次循环,判断以下一个物品为起点code: 
//题意是在所有点中寻找一个点,使得这个点到其他所有点的最短路径中最大的那条边长度是和其他相比最小的#include <stdio.h>#include <string.h>#define INF 0x3f3f3f3fint n,m;int mp[105][105];void Floyd(){int i,j,k;for(k = 1; k <= n; k++){for(i = 1; i <= n; i++){for(j = 1; j <= n; j++){if(mp[i][k] != INF && mp[k][j] != INF && mp[i][j] > mp[i][k] + mp[k][j]){ mp[i][j] = mp[i][k] + mp[k][j];}}}}}int main(){int i,j;scanf("%d%d",&n,&m);memset(mp,INF,sizeof(mp));for(i = 0; i < m; i++){int a,b,d;scanf("%d%d%d",&a,&b,&d);mp[a][b] = mp[b][a] = d;//由题意可知正反都是可以的,是无向图 }Floyd();//弗洛伊德算法,求每个点到其他所有点的最短路径 int min = INF;int f = -1;//记录物品编号for(i = 1; i <= n; i++){//以每个点作为起点枚举 int max = 0;int flag = 1;for(j = 1; j <= n; j++){if(j == i)//自己不能变成自己,所以跳过    continue;if(mp[i][j] == INF){//如果有不能变的,说明这个不行 flag = 0;break;}if(mp[i][j]>max){//寻找以i为起点到其他各点的最短路径中最长的 max = mp[i][j];}}if(!flag)   continue;else{if(max != 0 && max < min){//如果这条以i为起点的最长路径和以其他点为起点的最长路径比短,更新min,记录物品编号 min = max;f = i;    }} }if(min==INF)printf("0\n");else{printf("%d %d\n",f,min);}return 0;} 


原创粉丝点击