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 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

这道题的题意就是求从1到n的道路上的最大的最小值,也就是从1到n有多条路,每一条路上都有一个最小的权值,我们求的就是这个最小权值的最大值。不理解的看看样例就行了。让我费解的就是为啥全是用的迪杰斯特拉写的,我就纳闷了,难道spfa不能写?于是经过一段时间的挣扎,还是过了,对spfa的改变就是不能从起点开始更新了,而是要用与起点相连的那些点来更新,这一点我也是看了dijkstra才改的,可能与dijkstra有相同的地方吧。另外初始化也是有不少改动的,具体请看代码。

AC代码:

//
//  Created by  CQU_CST_WuErli
//  Copyright (c) 2015 CQU_CST_WuErli. All rights reserved.
//
// #include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <sstream>
#define CLR(x) memset(x,0,sizeof(x))
#define OFF(x) memset(x,-1,sizeof(x))
#define MEM(x,a) memset((x),(a),sizeof(x))
#define ALL(x) x.begin(),x.end()
#define AT(i,v) for (auto &i:v)
#define For_UVa if (kase!=1) cout << endl
#define BUG cout << "I am here" << endl
#define lookln(x) cout << #x << "=" << x << endl
#define look(x) cout << #x << "=" << x
#define SI(a) scanf("%d",&a)
#define SII(a,b) scanf("%d%d",&a,&b)
#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define root 1,n,1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define BigInteger bign
const int MAX_L=2005;// For BigInteger
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const double eps=1e-9;
const double pi=acos(-1);
typedef long long  ll;
using namespace std;

const int N=1e6+10;
int pnt[N],nxt[N],head[N],w[N];
int cnt;
int n,m;
int d[N];
void add_edge(int u,int v,int c){
    pnt[cnt]=v;nxt[cnt]=head[u];head[u]=cnt;
    w[cnt++]=c;
}


int vis[N];
int spfa(){
    CLR(vis);
    queue<int> q;
    for (int i=head[1];~i;i=nxt[i]){
        int v=pnt[i];
        q.push(v);
        d[v]=w[i];
        vis[v]=1;
    }
    while (!q.empty()){
        int x=q.front();q.pop();
        vis[x]=0;
        for (int i=head[x];~i;i=nxt[i]){
            int v=pnt[i];
            if (v==x) continue;
//          lookln(v);
            if (d[v]<min(d[x],w[i])){
                d[v]=min(d[x],w[i]);
                if (!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return d[n];
}

int main(){
#ifdef LOCAL
    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);
//  freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);
#endif
    int _;
    for (int kase=scanf("%d",&_);kase<=_;kase++){
        cnt=0;OFF(head);
        CLR(d);
        cin >> n >> m;
        for (int i=1;i<=m;i++){
            int u,v,c;
            SIII(u,v,c);
            add_edge(u,v,c);
            add_edge(v,u,c);
        }   
        printf("Scenario #%d:\n",kase);
        cout << spfa() << endl << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值