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[][];
int main()
{
int a,b,pri;
scanf("%d%d",&n,&m);
for(int i = ;i <= n;i ++)
{
for(int j = ;j <= n;j ++)
{
e[i][j] = inf;
}
}
for(int i = ;i < m;i ++)
{
scanf("%d%d%d",&a,&b,&pri);
e[a][b] = e[b][a] = pri;
}
for(int i = ;i <= n;i ++)
{
for(int j = ;j <= n;j ++)
{
for(int k = ;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 = -;
for(int i = ;i <= n;i ++)
{
int max = -,j;
for(j = ;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 == -)printf("");
else printf("%d %d",which,min);
}

7-26 Harry Potter's Exam(25 分)的更多相关文章

  1. PTA 09-排序1 排序 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/720 5-12 排序   (25分) 给定NN个(长整型范围内的)整数,要求输出从小到大 ...

  2. PTA 05-树7 堆中的路径 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径   (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...

  3. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  4. L2-001 紧急救援 (25 分)

    L2-001 紧急救援 (25 分)   作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...

  5. 1062 Talent and Virtue (25 分)

    1062 Talent and Virtue (25 分) About 900 years ago, a Chinese philosopher Sima Guang wrote a history ...

  6. 1006 Sign In and Sign Out (25 分)

    1006 Sign In and Sign Out (25 分) At the beginning of every day, the first person who signs in the co ...

  7. PTA 11-散列3 QQ帐户的申请与登陆 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/723 5-15 QQ帐户的申请与登陆   (25分) 实现QQ新帐户申请和老帐户登陆的简 ...

  8. PTA 11-散列2 Hashing (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing   (25分) The task of this pro ...

  9. PTA 11-散列1 电话聊天狂人 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/722 5-14 电话聊天狂人   (25分) 给定大量手机用户通话记录,找出其中通话次数 ...

  10. PTA 10-排序6 Sort with Swap(0, i) (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/678 5-16 Sort with Swap(0, i)   (25分) Given a ...

随机推荐

  1. 3.8 Templates -- Actions

    一.The {{action}} Helper 你的应用程序通常会需要一种方法来让用户用控件交互改变应用程序状态. 例如,你有一个显示blog post的模板,并支持用额外的信息扩展post. 可以使 ...

  2. (转)SSIS_数据流转换(Union All&合并联接&合并)

    Union All : 与sql语言 Union All 一样,不用排序,上下合并多个表.Union All转换替代合并转换:输入输出无需排序,合并超过两个表 合并联接 : 有左连接.内连接.完全连接 ...

  3. 2016-ccf-data-mining-competition 搜狗用户画像构建

    想法1:   分成147(3*7*7)类, 后来觉得这样效果不好,后来看了看竞赛要求的也是分别预测,分别评分,而不是一次就把3类的标签都给出   所有后来我们改进了当时的想法,决定对年龄,性别,学历进 ...

  4. zw版【转发·台湾nvp系列Delphi例程】HALCON MoveRectangle

    zw版[转发·台湾nvp系列Delphi例程]HALCON MoveRectangle procedure TForm1.Button1Click(Sender: TObject);var img : ...

  5. jQuery中的prop和attr区别

    最近在做一个项目用jq时发现一个问题  在谷歌中可以正常出效果  但是在火狐中就是不行 就是这个prop和attr   之前用的是attr方法   但是在火狐中不出效果  于是特意看了两者的区别 主要 ...

  6. spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...

  7. HDU1087

    /*记忆化dfs+dp,因为每次最多走k步,所以上下左右的方向有所扩展, dp[i][j]存的是从dp[i][j]出发能吃的最大个数*/ #include<stdio.h> #includ ...

  8. 常用php操作redis命令整理(四)SET类型

    SADD 将一个或多个member元素加入到集合key当中.(从左侧插入,最后插入的元素在0位置),集合中已经存在TK 则返回false,不存在添加成功 返回true <?php var_dum ...

  9. Ubuntu安装 Spark2.3.0 报错原因及解决

    Ubuntu 安装Spark出现的问题及解决 最近在搭建Hadoop集群环境和Spark集群环境,出现的问题可能不太复杂,纯粹记录安装步骤和问题解决办法.集群环境使用的是(2台)阿里云主机,操作系统是 ...

  10. UVA 11019 Matrix Matcher(二维hash + 尺取)题解

    题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...