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;
}
### 关于哈利波特主题的PTA C++编程题目 针对特定主题如“哈利波特”的编程练习,在 PTA 平台上的官方分类中可能不会直接以该名称作为标签。然而,可以建议通过一些具有故事背景的任务来模拟此类场景。 对于希望找到有魔法学校情境设定的编程挑战,推荐关注以下几个方面: #### 数据结构应用实例 - **霍格沃茨学院分院帽**:设计一个程序实现新生入学时根据性格特征分配到不同学院的功能。这可以通过定义学生类及其属性,并利用条件判断语句完成逻辑处理[^1]。 ```cpp #include <iostream> using namespace std; class Student { public: string name; int bravery, intelligence, loyalty; // 特质评分 void assignHouse() { if(bravery >= 8 && intelligence < 7){ cout << "Gryffindor"; }else if(intelligence >= 8){ cout << "Ravenclaw"; } // 更多分支... } }; ``` #### 算法实践案例 - **魔药配制优化问题**:给定一系列原材料以及所需数量,计算最小成本购买方案。此题涉及背包算法种,适合用于训练动态规划技巧[^2]。 ```cpp // 动态规划求解最低花费 int minCostToBuyIngredients(vector<int>& prices, vector<vector<int>>& needs) { map<vector<int>, int> memo; function<int(const vector<int>&)> dfs = [&](const vector<int>& remainNeeds)->int{ auto it = memo.find(remainNeeds); if(it != end(memo)) return (*it).second; bool allZero = true; for(auto need : remainNeeds) {if(need!=0){allZero=false;break;}} if(allZero) return 0; int res = INT_MAX / 2; for (auto& offer : offers) { vector<int> newRemainNeeds = remainNeeds; bool validOffer = true; for (size_t i=0;i<newRemainNeeds.size();i++) { newRemainNeeds[i]-=offer[i]; if(newRemainNeeds[i]<0) {validOffer=false;break;} } if(validOffer) res=min(res,dfs(newRemainNeeds)+prices[idx]); } memo.insert({remainNeeds,res}); return res; }; } ``` 尽管上述例子并非严格意义上的“哈利波特”专题试题,但是它们确实融入了一些与原著相似的情节元素,使得学习过程更加有趣味性和沉浸感。同时这些题目也涵盖了重要的计算机科学概和技术要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值