7-6 Harry Potter's Exam(25 分)(floyd最短路)

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

//题意是在所有点中寻找一个点,使得这个点到其他所有点的最短路径中最大的那条边长度是和其他相比最小的
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
int 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][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;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值