PTA数据结构与算法题目集(中文) 7-8

PTA数据结构与算法题目集(中文)  7-8

7-8 哈利·波特的考试 (25 分)
 

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

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

输入格式:

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

输出格式:

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

题目分析:一道考察图的最小路径问题 这道题是利用多源最短路径的计算方法Floyd算法来解决 注意Floyd算法中 初始化时对角线元素初始化为0 其它元素初始化为无穷大
  1 #define _CRT_SECURE_NO_WARNINGS   
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<malloc.h>
  5 #define MaxVertexNum 100
  6 #define INIFITY 65535
  7 typedef struct ENode* Edge;
  8 struct ENode
  9 {
 10     int V1, V2;
 11     int Weight;
 12 };
 13 
 14 typedef struct GNode* Graph;
 15 struct GNode
 16 {
 17     int Nv;
 18     int Ne;
 19     int G[MaxVertexNum][MaxVertexNum];
 20 };
 21 
 22 Graph BuildGraph(int VertexNum)
 23 {
 24     Graph Gra = (Graph)malloc(sizeof(struct GNode));
 25     Gra->Nv = VertexNum;
 26     Gra->Ne = 0;
 27     for (int i = 1; i <=Gra->Nv; i++)
 28         for (int j = 1; j <= Gra->Nv; j++)
 29         {
 30             Gra->G[i][j] = INIFITY;
 31             if (i == j)Gra->G[i][j] = 0;
 32         }
 33     return Gra;
 34 }
 35 
 36 void Insert(Edge E, Graph Gra)
 37 {
 38     Gra->G[E->V1][E->V2] = E->Weight;
 39     Gra->G[E->V2][E->V1] = E->Weight;
 40 }
 41 
 42 Graph CreateGraph()
 43 {
 44     Edge E = (Edge)malloc(sizeof(struct ENode));
 45     int N, M;
 46     scanf("%d%d", &N, &M);
 47     Graph G = BuildGraph(N);
 48     G->Ne = M;
 49     for (int i = 0; i < G->Ne; i++)
 50     {
 51         scanf("%d%d%d", &(E->V1), &(E->V2), &(E->Weight));
 52         Insert(E, G);
 53     }
 54     return G;
 55 }
 56 
 57 int Dist[100][100];
 58 int Path[100][100];
 59 int Floyd(Graph Gra)
 60 {
 61     for(int i=1;i<=Gra->Nv;i++)
 62         for (int j = 1; j <= Gra->Nv; j++)
 63         {
 64             Dist[i][j] = Gra->G[i][j];
 65             Path[i][j] = -1;
 66         }
 67 
 68     for(int k=1;k<=Gra->Nv;k++)
 69         for(int i=1;i<=Gra->Nv;i++)
 70             for (int j = 1; j <= Gra->Nv; j++)
 71                 if (Dist[i][k] + Dist[k][j] <Dist[i][j])
 72                 {
 73                     Dist[i][j]= Dist[i][k] + Dist[k][j];
 74                     if (i == j && Dist[i][j] < 0)
 75                         return 0;
 76                     Path[i][j] = k;
 77                 }
 78     return 1;
 79 }
 80 int Max[100];
 81 void Judge(Graph G)
 82 {
 83     for (int i = 1; i <= G->Nv; i++)
 84     {
 85         Max[i] = Dist[i][1];
 86         for (int j = 2; j <= G->Nv; j++)
 87         {
 88             if (Max[i] < Dist[i][j])
 89                 Max[i] = Dist[i][j];
 90         }
 91     }
 92     int Min=Max[1];
 93     int M = 1;
 94     for (int j = 2; j <= G->Nv; j++)
 95     {
 96         if (Min > Max[j])
 97         {
 98             Min = Max[j];
 99             M = j;    
100         }
101     }
102     for(int i=1;i<=G->Nv;i++)
103         if (Dist[M][i] == INIFITY)
104         {
105             printf("0");
106             return;
107         }
108     printf("%d %d", M, Min);
109 }
110 int main()
111 {
112     Graph G = CreateGraph();
113     if (Floyd(G))
114         Judge(G);
115     else
116         printf("0");
117     return 0;
118 }
View Code
 

猜你喜欢

转载自www.cnblogs.com/57one/p/11587072.html