C - Heavy Transportation POJ - 1797

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:\n", 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

时间: 2024-10-13 03:34:20

C - Heavy Transportation POJ - 1797的相关文章

Heavy Transportation POJ 1797 最短路变形

Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你求从编号为1的城市到编号为n的城市的路线中,最大能经过多重的车. 解题思路 这个题可以使用最短路的思路,不过转移方程变了\(dis[j]=max(dis[j], min(dis[u], e[u][j]))\).这里dis[j]表示从标号为1的点到达编号为j的点的路径中,最小的承重能力,就像短板效应样

Heavy Transportation POJ - 1797

#include<iostream> #include<algorithm> #include<queue> #include<map> #include<cstring> #include<cstring> #include<cstdio> #include<math.h> using namespace std; const int N=1e6+100; int h[N],w[N],ne[N],e[N],i

POJ 1797 Heavy Transportation (Dijkstra变形)

F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand busines

POJ 1797 Heavy Transportation SPFA变形

原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 24576   Accepted: 6510 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand

POJ 1797 Heavy Transportation (最短路变形)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 20364   Accepted: 5401 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

poj 1797 Heavy Transportation(最大生成树)

poj 1797 Heavy Transportation Description 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

POJ 1797 Heavy Transportation 【最大生成树,Prime】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 29765   Accepted: 7944 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ 1797 Heavy Transportation【Dijkstra最短路变形】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 29682   Accepted: 7919 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ 1797 Heavy Transportation (Dijkstra)

题目链接:POJ 1797 Description 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