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

#include <iostream>
#include <cstdio>
#define N 105
#define INF 0x3f3f3f3f;
using namespace std;

int g[N][N];
int n, m;

int main()
{
     //读取输入
    cin >> n >> m;
    
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if(i == j) g[i][j] = 0;
            else g[i][j] =999;
    
    for(int i = 1; i <= m; ++i) 
    {
        int a, b, x;
        cin >> a >> b >> x;
        g[a][b] = x;
        g[b][a] = x;
    }
    

 
        
    //floyd
    for(int k = 1; k <= n; ++k)
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    
    int idx = -1, res = 999;
    int maxn = 0;
    for(int i = 1; i <= n; ++i)
    {
        maxn = 0;
        for(int j = 1; j <= n; ++j)
            if(maxn < g[i][j])  //找到一条以这个点为起点,到其他点最长的路
                maxn = g[i][j];
        
        if(res > maxn)
        {
            res = maxn, idx = i;    //在最长的路里面找最短的路
        }
    }
    
    if(idx == -1) cout << "0" << endl;
    else printf("%d %d", idx, res);

            
    return 0;
}

上一篇:25 String 对象中的属性


下一篇:运行sh文件报错:转换sh文件为unix格式