Heavy Transportation Dijkstra

来源:互联网 发布:java福利院管理系统 编辑:程序博客网 时间:2024/06/05 14:20
Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem

You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.


Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.


Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.


Sample Input
1
3 3
1 2 3
1 3 4

2 3 5


Sample Output
Scenario #1:

4


哈哈哈,这道题和上一题很像啊,题意我直接猜的,和上一题正好相反,这道题是求在一条路径中,道路所能承受的最小重量要在所有路径中最大,联系下实际,就是这么回事~ 

做法一样,还是变异的迪杰2333,就是在if判断那里与上一题相反

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int N = 1010; int g[N][N], d[N];bool vis[N];int n, m;void Dijkstra(){    for(int i = 1; i <= n; i++)    {        d[i] = g[1][i];        vis[i] = 0;    }    vis[1] = 1;    for(int i = 2; i <= n; i++)    {        int u = -1, MAX = -1;        for(int j = 2; j <= n; j++)        {            if(!vis[j] && d[j] > MAX)     //注意,根据题意这里选的是最大承重            {                u = j;                MAX = d[j];            }        }        if(u == -1) return;        vis[u] = 1;        for(int v = 2; v <= n; v++)        {            if(!vis[v] && min(g[u][v], d[u]) > d[v])                 d[v] = min(g[u][v], d[u]);     //选取最短路径中的最小承重        }    }}int main(){    int t, u, v, w;    scanf("%d", &t);    for(int k = 1; k <= t; k++)    {        scanf("%d%d", &n, &m);        memset(g, 0, sizeof(g));        while(m--)        {            scanf("%d%d%d", &u, &v, &w);            g[u][v] = g[v][u] = w;        }        Dijkstra();        printf("Scenario #%d:\n", k);        printf("%d\n\n", d[n]);    }    return 0;}