哈利·波特的考试

  • 题目来源:

浙江大学在慕课网上开设的《数据结构》课,陈越老师、何钦铭老师主讲,课后作业的一道题。

  • 题目描述

1327401-20190820165055864-1938962488.png

1327401-20190820165103102-1184797018.png

  • 题目思路:

这个题目是求图中“每一对顶点之间的最短路径”,应用到的算法是Floyd算法。

  • C语言实现:
#define _CRT_SECURE_NO_WARNINGS

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

#define MaxVerterNum 101  //最大定点数设为100
#define INFINITY 65535  //无穷设为双字节无符号整数的最大值65535

typedef int WeightType;  //边的权值设为int类型
typedef char DataType;  //顶点存储的数据类型设置为字符类型
typedef int Vertex;  //用顶点下标表示顶点,为整型

struct GNode
{
    int Nv;  //顶点数
    int Ne;  //边数
    WeightType G[MaxVerterNum][MaxVerterNum];  //邻接矩阵
    DataType Data[MaxVerterNum][MaxVerterNum];  //存顶点的数据
    /*如果顶点无数据,此时Data[]可以不出现*/
};
//描述边的类型
struct ENode
{
    Vertex V1, V2;  //有向边<V1,V2>
    WeightType Weight;  //权重
};
//作用:初始化一个有VertexNum个顶点但没有边的图
struct GNode* CreateGraph(int VertexNum)
{
    Vertex V, W;
    struct GNode* Graph;
    Graph = (struct GNode*)malloc(sizeof(struct GNode));  //建立图
    Graph->Nv = VertexNum;
    Graph->Ne = 0;

    //初始化邻接矩阵
    for (V = 0; V < Graph->Nv; V++)
    {
        for (W = 0; W < Graph->Nv; W++)
        {
            Graph->G[V][W] = INFINITY;
        }
    }

    for (V = 0; V < Graph->Nv; V++)
    {
        for (W = 0; W < Graph->Nv; W++)
        {
            if (V == W)
            {
                Graph->G[V][W] = 0;  //对角线初始化为0
                break;
            }
        }
    }
    return Graph;
}

//作用:在图中插入边
void InsertEdge(struct GNode* Graph, struct ENode* E)
{
    插入边<V1,V2>
    Graph->G[E->V1][E->V2] = E->Weight;
    若是无向图,还需要插入<V2,V1>
    Graph->G[E->V2][E->V1] = E->Weight;

    插入边<V1,V2>
    //Graph->G[E->V1][E->V2] = 1;
    若是无向图,还需要插入<V2,V1>
    //Graph->G[E->V2][E->V1] = 1;
}
//作用:构建一个图,供主函数调用
struct GNode* BulidGraph()
{
    Vertex V;
    int NV;
    struct ENode* E;
    struct GNode* Graph;
    scanf("%d", &NV);  //读入顶点个数
    Graph = CreateGraph(NV);
    scanf("%d", &(Graph->Ne));  //读入边数

    if (Graph->Ne != 0)
    {
        E = (struct ENode*)malloc(sizeof(struct ENode));
        for (int i = 0; i < Graph->Ne; i++)
        {
            //如果权重不是整型,Weight的读入格式要改
            scanf("%d %d %d",&E->V1,&E->V2,&E->Weight);
            E->V1--;
            E->V2--;
            InsertEdge(Graph, E);
        }
    }

    如果顶点有数据的话,读入数据
    //for (V = 0;V < Graph->Nv;V++)
    //{
    //  scanf("%c",&(Graph->Data[V]));
    //}

    return Graph;
}

//作用:Floyd算法
//参数:struct GNode* Graph 图
//      WeightType D[][MaxVerterNum] 最短路径长度
//      Vertex path[][MaxVerterNum] 最短路径
bool Floyd(struct GNode* Graph,WeightType D[][MaxVerterNum],Vertex path[][MaxVerterNum])
{
    int i, j, k;

    for (i = 0;i < Graph->Nv;i++)
    {
        for (j = 0;j < Graph->Nv;j++)
        {
            D[i][j] = Graph->G[i][j];
            path[i][j] = -1;
        }
    }

    for (k = 0;k < Graph->Nv;k++)
    {
        for (i = 0; i < Graph->Nv; i++)
        {
            for (j = 0; j < Graph->Nv; j++)
            {
                if (D[i][k] + D[k][j] < D[i][j])
                {
                    D[i][j] = D[i][k] + D[k][j];
                    if (i == j && D[i][j] < 0)  //若发现负值圈
                    {
                        return false;
                    }
                    path[i][j] = k;
                }
            }
        }
    }
    return true;
}

int main()
{
    int result = INFINITY;
    int Animal = 0;
    struct GNode* Graph = BulidGraph();
    WeightType D[MaxVerterNum][MaxVerterNum];
    Vertex path[MaxVerterNum][MaxVerterNum];
    if (Floyd(Graph,D,path))
    {
        int temp = 0;
        for (int i = 0;i < Graph->Nv;i++)
        {
            for (int j = 0;j < Graph->Nv;j++)
            {
                if (D[i][j] == INFINITY)
                {
                    printf("0");
                    return 0;
                }
                if (D[i][j] > temp)
                {
                    temp = D[i][j];
                }
            }
            if (temp < result)
            {
                result = temp;
                Animal = i;
            }
            temp = 0;
        }
    }
    printf("%d %d\n",Animal + 1,result);
    // system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/Manual-Linux/p/11383978.html

根据提供的引用内容,我们可以了解到《哈利波特》是一部关于哈利、赫敏、罗恩等人在大法师邓布利多的帮助下,使用魔法抵抗伏地魔的故事。同时,根据引用和引用,我们可以使用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、付费专栏及课程。

余额充值