07-图4 哈利·波特的考试

题目地址:07-图4 哈利·波特的考试(25分)

求解过程:本文参考自MOOC,通过邻接矩阵和Floyd算法求解,主要分成以下几个模块
1.    BuildGraph()                  建立图
2.    Floyd(Graph)                 求两两之间的最短路径并直接在原图上更新
3.    FindAnimal(Graph)        找最适合带的动物
另外还加入了show()方法,方便新手对中间矩阵变化过程进行查看

程序

#include <stdio.h>
#include <stdlib.h>

#define MAXVERTIX 101
#define INFINITY  65536
typedef struct ENode* Edge;
struct ENode	// 定义边结点
{
	int weight;
	int v1, v2;
};

typedef struct GNode* MGraph;
struct GNode
{
	int Ne;	// 边数
	int Nv;	// 顶点数
	int G[MAXVERTIX][MAXVERTIX];	// 领接矩阵
};

MGraph CreateGraph(int VertixNum)	// 初始化图
{
	MGraph Graph = (MGraph)malloc(sizeof(GNode));
	Graph->Nv = VertixNum;
	Graph->Ne = 0;
	for (int i = 0; i < VertixNum; i++)
		for (int j = 0; j < VertixNum; j++)
		{
			if (i == j)		// 对角元赋值为0
				Graph->G[i][j] = 0;
			else
				Graph->G[i][j] = INFINITY;
		}
	return Graph;
}

void InsertEdge(MGraph Graph, Edge E)
{	/* 因为动物下标从1开始,矩阵下标从0开始 */
	Graph->G[--E->v1][--E->v2] = E->weight;
	Graph->G[E->v2][E->v1] = E->weight;
}

MGraph BuildGraph()		// 插入边,建立图
{
	int Nv;
	scanf("%d", &Nv);
	MGraph Graph = CreateGraph(Nv);
	scanf("%d", &Graph->Ne);
	Edge E = (Edge)malloc(sizeof(ENode));
	if (Graph->Ne)	// 如果存在边
		for (int i = 0; i < Graph->Ne; i++)
		{
			scanf("%d %d %d", &E->v1, &E->v2, &E->weight);
			InsertEdge(Graph, E);
		}
	return Graph;
}

void Floyd(MGraph Graph)	// Floyd算法直接应用于原图
{
	for (int k = 0; k < Graph->Nv; k++)
		for (int i = 0; i < Graph->Nv; i++)
			for (int j = 0; j < Graph->Nv; j++)
				if (Graph->G[i][k] + Graph->G[k][j] < Graph->G[i][j])
					Graph->G[i][j] = Graph->G[i][k] + Graph->G[k][j];
}

// void show(MGraph Graph)		// 显示矩阵
// {
// 	for (int i = 0; i < Graph->Nv; i++)
// 	{
// 		for (int j = 0; j < Graph->Nv; j++)
// 			printf("%d 		", Graph->G[i][j]);
// 		printf("\n");
// 	}
		
// }

void FindAnimal(MGraph Graph)	// 寻找最合适的动物
{
	int ID;	// 保存动物ID
	int MinMaxLen = INFINITY;
	for (int i = 0; i < Graph->Nv; i++)
	{	int MaxLen = 0;		// 这个得放进循环里面
		for (int j = 0; j < Graph->Nv; j++)
		{	/* 如果i,j不连通说明带一只动物是不够的 */
			if (Graph->G[i][j] == INFINITY)
			{
				printf("0\n");
				return;
			}
			if (Graph->G[i][j] > MaxLen)	// 找一行中最长的路径
				MaxLen = Graph->G[i][j];
		}
		if (MaxLen < MinMaxLen)	// 找所有最长路径中最短的路径
		{
			MinMaxLen = MaxLen;
			ID = i+1;
		}
	}
	printf("%d %d\n", ID, MinMaxLen);
}

int main(int argc, char const *argv[])
{
	MGraph Graph = BuildGraph();
	// show(Graph);
	// printf("======================================\n");
	Floyd(Graph);
	// show(Graph);
	FindAnimal(Graph);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值