题目:http://www.lightoj.com/volume_showproblem.php?problem=1002

题意:给定无向图,求从某点出发到所有的点的最短路径,本题中两点间最短路径的定义为:两点间所有路径中最长边的最小值

思路:最短路变形,求最短路时稍微变一下条件即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define debug() puts("here")
using namespace std;

const int N = 510;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to, cost, next;
}g[N*N];
int cas;
int cnt, head[N];
bool vis[N];
int dis[N];
void add_edge(int v, int u, int cost)
{
    g[cnt].to = u, g[cnt].cost = cost, g[cnt].next = head[v], head[v] = cnt++;
}
void spfa(int s)
{
    memset(vis, 0, sizeof vis);
    memset(dis, 0x3f, sizeof dis);
    queue<int> que;
    que.push(s);
    dis[s] = 0, vis[s] = true;
    while(! que.empty())
    {
        int v = que.front(); que.pop();
        vis[v] = false;
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(dis[u] > max(dis[v], g[i].cost)) //改变在此处
            {
                dis[u] = max(dis[v], g[i].cost);
                if(! vis[u])
                    que.push(u), vis[u] = true;
            }
        }
    }
}
int main()
{
    int t, n, m, a, b, c, d;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        cnt = 0;
        memset(head, -1, sizeof head);
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            add_edge(a, b, c);
            add_edge(b, a, c);
        }
        scanf("%d", &d);
        spfa(d);
        printf("Case %d:\n", ++cas);
        for(int i = 0; i < n; i++)
            if(dis[i] == INF) printf("Impossible\n");
            else printf("%d\n", dis[i]);
    }
    return 0;
}