当前位置: 代码迷 >> 综合 >> POJ 1797 Heavy Transportation (最短路 / 求路径最小边)
  详细解决方案

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

热度:87   发布时间:2023-12-22 13:48:25.0

传送门

题意: 有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;
}