哈利波特魔法题——弗洛伊德最短路径算法

import java.util.Scanner;
public class 多源最短路径算法 {
/*
* 输入第1行给出两个正整数N (≤100)和M,其中N是考试涉及的动物总数,
* M是用于直接变形的魔咒条数。为简单起见,我们将动物按1~N编号。随后M行,
* 每行给出了3个正整数,分别是两种动物的编号、以及它们之间变形需要的魔咒的长度
* 数字之间用空格分隔。
输出格式:
输出哈利·波特应该带去考场的动物的编号、以及最长的变形魔咒的长度,中间以空格分隔。
如果只带1只动物是不可能完成所有变形要求的,则输出0。如果有若干只动物都可以备选,
则输出编号最小的那只。
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
*/

	static int N;
	static int E;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		N = in.nextInt();//动物数
		E = in.nextInt();//有E行数据
		
		int G [][] = new int[N][N];//初始化图,无穷大
 
		for(int i =0; i<N; i++) {
			for(int j = 0; j<N; j++) {
				G[i][j] = Integer.MAX_VALUE/10;
				//防止弗洛伊德算法出现相加溢出
			}
		}
		
		for(int i=0;i<E;i++) {    //插入边
			int m = in.nextInt()-1;//起点
			int n = in.nextInt()-1;//中间点				
			int weight = in.nextInt();//代价值
			G[m][n] = weight;
			G[n][m] = weight;
		}
		
		for(int k = 0;k<N;k++)	//Floyd算法,更新了最短路径矩阵D
			for(int i =0;i<N;i++)
				for(int j = 0;j<N; j++) {
						G[i][j]=Math.max(G[i][j], G[i][k]+G[k][j]);						
				}
		int temp = Integer.MIN_VALUE;
		int dist [] = new int[N];
		
		for(int i =0; i<N;i++ ) {//求出D矩阵每一行中的最大值,存储在 len矩阵中
			for(int j = 0;j<N;j++) {//咒语的最大长度
				if(G[i][j]>temp&&j!=i) {
					temp = G[i][j];
				}
			}
			dist[i] = temp;
			temp = Integer.MIN_VALUE;
		}
		
		int res = Integer.MAX_VALUE/10;//代表路径
		int V = -1;//代表某只动物
		for(int i = 0; i<N; i++) {
			if(dist[i]<res) {
				res = dist[i];
				V = i;
			}
		}
		if(V<0)
			System.out.print(0);
		else
			System.out.print((V+1)+" "+res);					
	}	 
}`
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以了解到《哈利波特》是一部关于哈利、赫敏、罗恩等人在大法师邓布利多的帮助下,使用魔法抵抗伏地魔的故事。同时,根据引用和引用,我们可以使用Python对小说中的人物名字和出现频率进行统计和分析。 以下是Python代码示例: 1. 统计人物名字TOP20的词语 ```python import jieba import pandas as pd from collections import Counter from pyecharts import Bar # 读取小说文本 with open('harry_potter.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用jieba分词 words = jieba.lcut(text) # 统计人物名字出现的次数 names = ['哈利', '赫敏', '罗恩', '邓布利多', '马尔福', '斯内普', '小天狼星'] names_count = Counter([word for word in words if word in names]) # 绘制柱状图 bar = Bar('主要人物Top20', background_color='white', title_pos='center', title_text_size=20) x = names_count.most_common(20) bar.add('', [i[0] for i in x], [i[1] for i in x], xaxis_interval=0, xaxis_rotate=30, is_label_show=True) bar.render() ``` 2. 统计整部小说出现最多的词语TOP15 ```python import jieba import pandas as pd from collections import Counter # 读取小说文本 with open('harry_potter.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用jieba分词 words = jieba.lcut(text) # 统计词语出现的次数 words_count = Counter(words) # 去除停用词 stopwords = pd.read_csv('stopwords.txt', index_col=False, quoting=3, sep='\t', names=['stopword'], encoding='utf-8') words = [word for word in words if word not in stopwords] # 统计出现最多的词语TOP15 top15 = words_count.most_common(15) print(top15) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值