7-8 Harry Potter's Exam

Topic link:
Portal

Title description:
Harry Potter is about to take an exam, he needs your help. This course learns the ability to use magic spells to turn one animal into another. For example, the spell to turn a cat into a mouse is haha, the spell to turn a mouse into a fish is hehe, and so on. The curse that changes in the opposite direction is simply to reverse the original curse. For example, ahah can turn a mouse into a cat. In addition, if you want to turn a cat into a fish, you can recite a direct spell lalala, or you can link the spells of cats to mice and mice to fish: hahahehe.

Now Harry Potter has a textbook in his hand, which lists all the transformation spells and the animals that can be transformed. The teacher allowed him to bring an animal to the examination room by himself, to examine his ability to turn this animal into any designated animal. So he came to ask you: what animal to bring to make the most difficult animal (that is, the longest enchantment required for the animal to become Harry Potter's own) requires the shortest enchantment? For example: if there are only cats, mice, and fishes, then obviously Harry Potter should take the mouse, because the mouse only needs to read 4 characters when it becomes the other two animals; and if you take the cat, you need to read at least 6 Characters can turn a cat into a fish; for the same reason, taking a fish is not the best choice.

Input format:
Input description: Input the first line to give two positive integers N (≤100) and M, where N is the total number of animals involved in the test, and M is the number of spells used for direct transformation. For simplicity, we number the animals from 1 to N. In the following M lines, each line gives 3 positive integers, which are the numbers of the two animals and the length of the enchantment needed to transform between them (≤100). The numbers are separated by spaces.

Output format:
output the number of the animal Harry Potter should take to the examination room and the length of the longest transformation spell, separated by a space. If it is impossible to complete all the deformation requirements with only one animal, output 0. If there are several animals that can be selected, the one with the smallest number will be output.

Input example:
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

Sample schematic:
Insert picture description here
beginning adjacency matrix:
Insert picture description here
In a number of vertex points as an intermediary, update matrices:
Insert picture description here
in No. 2 mediated vertex point, to update the matrix:
Insert picture description here
to No. 3 as an intermediary vertex point matrix update:
Insert picture description here
In The vertex number 4 is the intermediate point, update the matrix:
Insert picture description here
take the vertex number 5 as the intermediate point, update the matrix:
Insert picture description here
take the vertex number 6 as the intermediate point, update the matrix: the
Insert picture description here
above is the flow of the Floyd algorithm!

Question idea:
Related applications of Floyd algorithm.

c reference code:

#include <stdio.h>
#include <string.h>//memset()函数头文件。

#define MAX 101
#define INF 0x3f3f3f3f//定义无穷大。

int n,m;
int G[MAX][MAX];

/*Floyd算法核心代码*/
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(G[i][j]>G[i][k]+G[k][j])
			  G[i][j]=G[i][k]+G[k][j];
		}
	}
}

int main()
{
    
    
	int i,j,x,y,d;
	scanf("%d%d",&n,&m);
	
	memset(G,INF,sizeof(G));//memset()函数对邻接矩阵初始化。
	for(i=1;i<=n;i++)
	 G[i][i]=0;//自己到自己本身距离初始化为0。不然用Floyd算法会出错。
	
	for(i=0;i<m;i++)
	{
    
    
		scanf("%d%d%d",&x,&y,&d);
		G[x][y]=G[y][x]=d;//无向图。
	}
	
	Floyd();
	
	int index=0;
	int min=INF;
	int max;
	
	for(i=1;i<=n;i++)
	{
    
    
		max=0;//每进行完一次循环后都要初始化。
		for(j=1;j<=n;j++)//找出编号为i的顶点到其他任意顶点最短路径的最大值。
		{
    
    
			if(G[i][j]>max)
			 max=G[i][j];
		}
		
		if(max<min)
		{
    
    
			min=max;//更新最长的变形魔咒的长度。
			index=i;//更新应该带去考场的动物的编号。
		}
	}
	
	//当有无法变成的动物时,max会等于INF。
	if(index==0)
	 printf("0");
	else
	 printf("%d %d",index,min);
	return 0;
}

Reference:
Portal

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/114478422