PAT : 数据结构与算法题目集(中文)7-8 哈利·波特的考试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/belous_zxy/article/details/88026603
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const auto INF = 0x3F3F3F3F;

int getdigit(void)
{
    int temp{0};
    char ch;
    while (1)
    {
        ch = getchar();
        if (ch == ' ' || ch == '\n' || ch == '\n')
            return temp;
        temp = temp * 10 + ch - '0';
    }
}
void putdigit(int temp)
{
    if (temp >= 10)
        putdigit(temp / 10);
    putchar('0' + (temp % 10));
}
int main(int argc, char *argv[])
{
    int animal{getdigit()}, cnt{getdigit()};
    int graph[101][101];
    for (int i = 1; i <= animal; ++i)
        for (int j = 1; j <= animal; ++j)
            graph[i][j] = INF;
    while (cnt--)
    {
        int a{getdigit()}, b{getdigit()}, value{getdigit()};
        graph[a][b] = graph[b][a] = value;
    }
    for (int k = 1; k <= animal; ++k)
        for (int i = 1; i <= animal; ++i)
            for (int j = 1; j <= animal; ++j)
            {
                int temp = graph[i][k] + graph[k][j];
                if (temp < graph[i][j])
                    graph[i][j] = temp;
            }
    int Min{INF}, Index{0};
    for (int i = 1; i <= animal; ++i)
    {
        bool jump = false;
        int INMax{0};
        for (int j = 1; j <= animal; ++j)
        {
            if (i == j)
                continue;
            if (graph[i][j] == INF)
            {
                jump = true;
                break;
            }
            INMax = max(INMax, graph[i][j]);
        }
        if (jump)
            continue;
        if (INMax < Min)
        {
            Min = INMax;
            Index = i;
        }
    }
    if (!Index)
        putchar('0');
    else
    {
        putdigit(Index);
        putchar(' ');
        putdigit(Min);
    }
    putchar('\n');
    return EXIT_SUCCESS;
}

Floyd算法,之后找每行的最大值,然后找出所有行最大值的最小值。

 

END

猜你喜欢

转载自blog.csdn.net/belous_zxy/article/details/88026603
今日推荐