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

题目描述

哈利·波特要考试了,他需要你的帮助。这门课学的是用魔咒将一种动物变成另一种动物的本事。例如将猫变成老鼠的魔咒是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

解题思路

这道题很明显是一个多源有权图的最短路径问题,只需要根据输入构建邻接矩阵,然后调用Floyd算法,得到距离矩阵,找到每行中最大值的最小值即可。

代码

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

#define MAXSIZE 101
#define INFINITY 65535

struct Graph {
    int vertexCount;
    int edgeCount;
    int matrix[MAXSIZE][MAXSIZE];
};
typedef struct Graph *MGraph;

MGraph createGraph();
void findAnimal(MGraph graph);
MGraph floyd(MGraph graph);
int findMax(MGraph dis, int row, int count);

int main() {
    MGraph graph = createGraph();
    findAnimal(graph);
    return 0;
}   

//创建图
MGraph createGraph() {
    MGraph graph = (MGraph) malloc(sizeof(struct Graph));
    scanf("%d %d", &(graph->vertexCount), &(graph->edgeCount));
    //对角线上的元素初始化为0,其余的元素初始化为正无穷
    for (int i = 1; i <= graph->vertexCount; i++) {
        for (int j = 1; j <= graph->vertexCount; j++) {
            if (i == j) graph->matrix[i][j] = 0;
            else graph->matrix[i][j] = INFINITY;
        } 
    }
    //给边赋值
    for (int i = 0; i < graph->edgeCount; i++) {
        int x, y, weight;
        scanf("%d %d %d", &x, &y, &weight);
        graph->matrix[x][y] = weight;
        graph->matrix[y][x] = weight;
    }
    return graph;
}

void findAnimal(MGraph graph) {
    MGraph dis = floyd(graph);
    int min = INFINITY, max, animal;
    int n = dis->vertexCount;
    for (int row = 1; row <= n; row++) {
        max = findMax(dis, row, n);
        if (max == INFINITY) {  //有顶点未连通,打印0,提前返回
            printf("0\n");
            return;
        }
        if (max < min) {    //更新最小值和动物标号
            min = max;
            animal = row;
        }
    }
    printf("%d %d\n", animal, min);
}

//floyd算法,传入邻接矩阵,返回距离矩阵
MGraph floyd(MGraph graph) {
    MGraph dis = (MGraph) malloc(sizeof(struct Graph));
    dis->vertexCount = graph->vertexCount;
    dis->edgeCount = graph->edgeCount;
    int n = graph->vertexCount;

    //将距离矩阵初始化为邻接矩阵
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            dis->matrix[i][j] = graph->matrix[i][j];
    
    //floyd算法主要逻辑
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (dis->matrix[i][k] + dis->matrix[k][j] < dis->matrix[i][j])
                    dis->matrix[i][j] = dis->matrix[i][k] + dis->matrix[k][j];
    
    return dis;
}

//在距离矩阵的一行中找到最大值
int findMax(MGraph dis, int row, int count) {
    int max = 0;
    for (int i = 1; i <= count; i++)
        if (dis->matrix[row][i] > max)
            max = dis->matrix[row][i];
    return max;
}

猜你喜欢

转载自www.cnblogs.com/AndyHY-Notes/p/12625657.html