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

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

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

输入格式:

输入说明:输入第1行给出两个正整数NN (\le 100≤100)和MM,其中NN是考试涉及的动物总数,MM是用于直接变形的魔咒条数。为简单起见,我们将动物按1~NN编号。随后MM行,每行给出了3个正整数,分别是两种动物的编号、以及它们之间变形需要的魔咒的长度(\le 100≤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>
#include<string.h>
#define MaxVertexNum 100            //最大顶点数; 
#define INFINITY 65535              //定义最大值;    
typedef int Vertex;
typedef int WeightType;
typedef struct ENode *PtrToENode;
struct ENode{
    Vertex V1,V2;                   //有向边<V1,V2>; 
    WeightType Weight;              //权重 
};
typedef PtrToENode Edge;
typedef struct GNode *PtrToGNode;
struct GNode{
    int Nv;                         //顶点数; 
    int Ne;                         //边数;                   
    WeightType G[MaxVertexNum][MaxVertexNum];       //邻接矩阵; 
};
typedef PtrToGNode MGraph;
MGraph CreateGraph (int VertexNum);
void InsertEdge(MGraph Graph,Edge E);
MGraph  BuildGraph();
void Floyd(MGraph Graph,WeightType D[][MaxVertexNum]);
WeightType FindMaxDist(WeightType D[][MaxVertexNum],Vertex i,int N); 
void FindAnimal(MGraph Graph); 
int main(){
    MGraph G = BuildGraph();
    FindAnimal(G);
    return 0;
}
MGraph CreateGraph (int VertexNum){
    Vertex v,w;
    MGraph Graph;
    Graph = (MGraph)malloc(sizeof(struct GNode));
    Graph->Nv = VertexNum;
    Graph->Ne = 0;
    for(v=0;v<VertexNum;v++){
        for(w=0;w<VertexNum;w++){
            Graph->G[v][w]=INFINITY;
        }
    }
    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;
    scanf("%d",&Nv);                    //读入顶点个数; 
    Graph = CreateGraph(Nv);            //初始化顶点个数为Nv的图; 
    scanf("%d",&(Graph->Ne));               //读入边的个数; 
    if(Graph->Ne!=0){                   //如果有边  
        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--;            //因为起始编号从零开始的; 
            InsertEdge(Graph,E);
        }
    }
    return Graph;
}
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];
                }
            }
        }
    }
}
void FindAnimal(MGraph Graph){
    WeightType D[MaxVertexNum][MaxVertexNum],MaxDist,MinDist;   
    Vertex Animal,i;
    Floyd(Graph,D);
    MinDist = INFINITY;
    for(i=0;i<Graph->Nv;i++){
        MaxDist = FindMaxDist(D,i,Graph->Nv);
        if(MaxDist==INFINITY){          //说明从i无法变出的动物; 
            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++){                 //找到i到其他动物的最长距离; 
        if(i!=j&&D[i][j]>MaxDist){
            MaxDist = D[i][j];
        }
    }
    return MaxDist;
}
  • 0
    点赞
  • 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、付费专栏及课程。

余额充值