C

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


和POJ2253那道题的青蛙跳反着来就可以了
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll  long long
#define PII  pair<int, int>
using namespace std;
int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)
//1000000000000000000

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

int n, m;
int g[N][N];
vector<int> d(N, -inf);
int dijkstra()
{
    vector<bool> vis(N, false);
    for (int i = 1; i <= n; i++)
        d[i] = g[i][1];
    d[1] = 0;
    vis[1] = 1;
    for (int i = 1; i < n; i++)
    {
        int maxi = -1, maxn = 0;
        for (int j = 1; j <= n; j++)//目前1到各点最少的承重是minn,这个节点是mini
        {
            if (!vis[j] && d[j]>maxn)
            {
                maxi = j;
                maxn = d[j];
            }
        }
        if (maxi == -1)
            break;
        vis[maxi] = 1;
        for (int j = 1; j <= n; j++)//更新d[j](1到mini的这条线路)
        {
            if (!vis[j])
            {
                d[j] = max(d[j],min(maxn, g[j][maxi]));
            }
        }
    }
    return d[n];
}

int main() 
{
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++)
    {
        printf("Scenario #%d:
", t);
        cin >> n >> m;
        memset(g,0,sizeof(g));
        for (int i = 1; i <= m; i++)
        {
            int u, v, w;
            cin >> u >> v >> w;
            g[u][v] = w;
            g[v][u] = w;
        }
        cout << dijkstra() << endl;
        cout << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/12683691.html