POJ 1797 Heavy Transportation (最短路 / 求路径最小边)

传送门

题意: 有n个交叉口,m条路,每条路有三个属性:起点,终点,最大载重。
假设从a到b的最大载重是从a -> b所能承载的最大重量,问从1 -> n的最大载重是多少?

输入: 第一行包含方案(城市计划)的数量。对于每个城市,第一行给出了路口的数量n(1 <= n <= 1000)和街道数量m。接下来的m行包含整数的三元组,这些整数指定了道路的起点和终点交叉点以及允许的最大权重(为正且不大于1000000)。每对交叉点之间最多有一条街道。

输出: 每个方案的输出都以包含“方案#i:”的行开头,其中i是从1开始的方案编号。然后打印一行,其中包含Hugo可以运输给客户的最大允许重量。用空白行终止方案的输出。
Sample Input

1
3 3
1 2 3
1 3 4
2 3 5
Sample Output

Scenario #1:
4

思路:

  • 其实和上一题(求路径最大边)非常相似,只不过上一题是求路径最小的最大跳跃距离,而这个题感觉是在求最小的最小承载重量。
  • 其实就是Dijkstra()一下,只不过上一题的d[i]表示0 -> i 的最小最大距离,而这一题的d[i]就表示1 -> i的最大承载重量。

代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <cmath>
#define null NULL
#define ll long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 1e60;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 1005;

int t, n, m, tt;
//用d数组来存路径中的最大承载(最小边)
int d[N], f[N][N];
bool vis[N];

int Dijkstra()
{
    me(vis);
    //先初始化d数组
    for(int i = 1; i <= n; i ++)
        d[i] = f[1][i];

    vis[1] = 1;

    for(int i = 1; i < n; i ++){
        int tmp = 0, pose = 0;
        //找到最小的边
        for(int j = 1; j <= n; j ++){
            if(!vis[j] && tmp < d[j]){
                tmp = d[j];
                pose = j;
            }
        }
        vis[pose] = 1;

        for(int j = 1; j <= n; j ++){
            if(!vis[j] && f[pose][j]){
                //将j加入集合的最大承载值
                tmp = min(d[pose], f[pose][j]);
                //扩大j的最大承载值
                d[j] = max(d[j], tmp);
            }
        }
    }

    return d[n];
}

int main()
{
    scanf("%d", &t);
    while(t --){
        scanf("%d%d", &n, &m);
        me(f); //记住要初始化
        for(int i = 0; i < m; i ++){
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            f[a][b] = f[b][a] = c;
        }

        printf("Scenario #%d:\n%d\n\n", ++ tt, Dijkstra());
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值