U167571 信使

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N = 103, M = 116300, INF = 0x3f3f3f3f;
typedef long long int LL;
typedef unsigned long long int ULL;
const int mod = 1000000007;
#define x first
#define y second
typedef pair<int, LL> PIL;
typedef pair<int, int> PII;
int n, m;
int g[N][N];
int floyd()
{
    for (int k = 1; k <= n; k++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (g[i][j] > g[i][k] + g[k][j])
                {
                    g[i][j] = g[i][k] + g[k][j];//利用floyd算法更新两个点之间的路径
                }
            }
                }
    }
    int res = 0;
    for (int i = 1; i <= n; i++)
    {
        if (g[1][i] == INF)
            return -1;
        res = max(res, g[1][i]);
    }//如果有个点到1(指挥部)的距离为INF,则说明有个地方信息传不到,输出-1,否则输出最晚传达到的阵营所需的时间。
    return res;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    memset(g, 0x3f, sizeof g);
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = g[b][a] = min(g[a][b], c);
    }
    int res = floyd();
    cout << res << endl;
    return 0;
}
相关推荐
CoderYanger7 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
CoderYanger15 小时前
动态规划算法-子序列问题(数组中不连续的一段):30.最长数对链
java·算法·leetcode·动态规划·1024程序员节
CoderYanger17 小时前
C.滑动窗口——2762. 不间断子数组
java·开发语言·数据结构·算法·leetcode·1024程序员节
智者知已应修善业1 天前
【输入两个数字,判断两数相乘是否等于各自逆序数相乘】2023-10-24
c语言·c++·经验分享·笔记·算法·1024程序员节
CoderYanger2 天前
动态规划算法-子数组、子串系列(数组中连续的一段):21.乘积最大子数组
开发语言·算法·leetcode·职场和发展·动态规划·1024程序员节
CoderYanger2 天前
A.每日一题——3432. 统计元素和差值为偶数的分区方案
java·数据结构·算法·leetcode·1024程序员节
CoderYanger2 天前
动态规划算法-子数组、子串系列(数组中连续的一段):26.环绕字符串中唯一的子字符串
java·算法·leetcode·动态规划·1024程序员节
韩家阿杰3 天前
RabbitMQ技术的使用
1024程序员节
CoderYanger3 天前
动态规划算法-简单多状态dp问题:15.买卖股票的最佳时机含冷冻期
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger3 天前
递归、搜索与回溯-FloodFill:33.太平洋大西洋水流问题
java·算法·leetcode·1024程序员节