07-图4 哈利·波特的考试 (25 分)

Floyd:https://www.cnblogs.com/skywang12345/p/3711523.html(超详细!超棒!

关于二维数据做参数的解答:https://www.cnblogs.com/Anker/archive/2013/03/09/2951878.html


这个代码根据老师的课程打很简单的。完全一模一样。

(问题)但是有个问题我还没弄懂,就是:为什么对角线上的一定要设无穷大而不能是0。如果我在建邻接矩阵时初始化为0的话 (即对角线是0,答案会出错),只有设成无穷大才正确。

 搞懂了。因为初始为无穷大有两个作用。我只看到了对角线为无穷,但是,初始化的时候无穷大,才到后面的插入边。就是说,如果没有边连通的话,它的值是属于无穷大的。而我当时想问的那个问题只注意到对角线能不能为0,但如果把初始化设为0,意味着没有连通的顶点值为0,那

void FindAnimal(MGraph Graph);

WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex i, int Nv);

void Floyd(MGraph Graph, WeightType D[][MaxVertexNum]);

这三个函数里面是要改的,不是说简单的把邻接矩阵初始化为0就可以了。刚刚试了一下,发现不太好。比初始化为无穷大要复杂。 


题目 

哈利·波特要考试了,他需要你的帮助。这门课学的是用魔咒将一种动物变成另一种动物的本事。例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等。反方向变化的魔咒就是简单地将原来的魔咒倒过来念,例如ahah可以将老鼠变成猫。另外,如果想把猫变成鱼,可以通过念一个直接魔咒lalala,也可以将猫变老鼠、老鼠变鱼的魔咒连起来念:hahahehe。

现在哈利·波特的手里有一本教材,里面列出了所有的变形魔咒和能变的动物。老师允许他自己带一只动物去考场,要考察他把这只动物变成任意一只指定动物的本事。于是他来问你:带什么动物去可以让最难变的那种动物(即该动物变为哈利·波特自己带去的动物所需要的魔咒最长)需要的魔咒最短?例如:如果只有猫、鼠、鱼,则显然哈利·波特应该带鼠去,因为鼠变成另外两种动物都只需要念4个字符;而如果带猫去,则至少需要念6个字符才能把猫变成鱼;同理,带鱼去也不是最好的选择。

输入格式:

输入说明:输入第1行给出两个正整数N (≤100)和M,其中N是考试涉及的动物总数,M是用于直接变形的魔咒条数。为简单起见,我们将动物按1~N编号。随后M行,每行给出了3个正整数,分别是两种动物的编号、以及它们之间变形需要的魔咒的长度(≤100),数字之间用空格分隔。

输出格式:

输出哈利·波特应该带去考场的动物的编号、以及最长的变形魔咒的长度,中间以空格分隔。如果只带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

输出样例:

4 70

代码如下:

#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 100
#define INFININY 65535
typedef int Vertex;
typedef int WeightType;

struct GNode{
    int Nv;
    int Ne;
    WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef struct GNode *PtrToGNode;
typedef PtrToGNode MGraph;

struct ENode{
    Vertex V1,V2;
    WeightType Weight;
};
typedef struct ENode *PtrToENode;
typedef PtrToENode Edge;

MGraph CreatGraph(int VertexNum);
void InsertEdge(MGraph Graph, Edge E);
MGraph BuildGraph();
void FindAnimal(MGraph Graph);
WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex i, int Nv);
void Floyd(MGraph Graph, WeightType D[][MaxVertexNum]);

int main(){
    MGraph Graph=BuildGraph();
    FindAnimal(Graph);

    return 0;
}
MGraph CreatGraph(int VertexNum){
    Vertex V,W;
    MGraph Graph=(MGraph)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]=INFININY;//0的话会出错

    return Graph;
}
void InsertEdge(MGraph Graph, Edge E){
    Graph->G[E->V1][E->V2]=E->Weight;
    Graph->G[E->V2][E->V1]=E->Weight;
}
MGraph BuildGraph(){
    MGraph Graph;
    Edge E; int Nv, i; //Vertex V;
    scanf("%d",&Nv);
    Graph=CreatGraph(Nv);
    scanf("%d",&Graph->Ne);
    if(Graph->Ne){
        E=(Edge)malloc(sizeof(struct ENode));
        for(i=0; i<Graph->Ne; i++){
            scanf("%d %d %d",&E->V1, &E->V2, &E->Weight);
            E->V1--;E->V2--;//animal begin from 1,but array begin from 0.the last output shoule +1
            InsertEdge(Graph, E); 
        }
    }
    return Graph;
}
void FindAnimal(MGraph Graph){
    Vertex i,Animal;
    WeightType D[MaxVertexNum][MaxVertexNum], MinDist, MaxDist;

    Floyd(Graph, D);

    MinDist=INFININY;
    for(i=0; i<Graph->Nv; i++){
        MaxDist=FindMaxDist(D, i, Graph->Nv);
        if(MaxDist==INFININY){//不连通
           printf("0\n");
           return;
        }
        if(MinDist>MaxDist){
            MinDist=MaxDist;
            Animal=i+1;
        }
    }
    printf("%d %d\n",Animal,MinDist);
}
WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex i, int N){
    WeightType MaxDist;
    Vertex j;
    MaxDist=0;
    for(j=0; j<N; j++)
     if(i!=j && MaxDist<D[i][j])
         MaxDist=D[i][j];

    return MaxDist;//如果图不连通,即第i行出现了INFININY,MaxDist势必会变成INFININY
}
void Floyd(MGraph Graph, WeightType D[][MaxVertexNum]){
    Vertex i, j, k;

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

    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];
           }
}

顺便码一个红黑树:https://www.cnblogs.com/skywang12345/p/3245399.html

  • 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、付费专栏及课程。

余额充值