poj 1797 Heavy Transportation 【最短路Dijkstra 变式】

Heavy Transportation

Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 23914 Accepted: 6355

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

题意&分析:水题一枚,给定N个点,M条路,求从1到N的最大的最小边权,如果数据在300以内,Floyd水过,但是这个题不行,N<1000,那么,我们就可以用Dijstra来做,这里需要注意的应该就是 初始化以及顶点的松弛操作:

d[y] = max(d[y], min(d[x], Map[x][y]));

网上有给最大生成树来做,也很简单,最小生成树会的话,这个也就不再话下。

下面给出两种风格的Dij代码:

/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define rep(i,a,b)      for(int i = a;i <= b;i++)
#define MP(a,b)         make_pair(a,b)
#define PB(a)           push_back(a)
#define fst             first
#define snd             second
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x)          cout<<x<<"  ";
/****************************>>>>SEPARATOR<<<<****************************/
const int maxn = 1000 + 5;
const int INF = 0x3f3f3f3f;
int N, M, Map[maxn][maxn], d[maxn];
bool vis[maxn];
void init()
{
    memset(Map, 0, sizeof(Map));
}
void Dijkstra()
{
    memset(vis, false, sizeof(vis));
    d[1] = 0;
    rep(i,2,N) d[i] = Map[1][i];
    rep(i, 1, N-1)
    {
        int x = 0, m = 0;
        rep(y, 1, N) if(!vis[y] && d[y] > m) m = d[x = y];
        vis[x] = true;
        rep(y, 1, N)
        {
            if(!Map[x][y]) continue;
            d[y] = max(d[y], min(d[x], Map[x][y]));
        }
    }
}
int main()
{
   // FIN;
    int T, cas = 0, x, y, t;
    for(scanf("%d", &T); T--;)
    {
        scanf("%d %d", &N, &M);
        init();
        rep(i, 1, M)
        {
            scanf("%d %d %d", &x, &y, &t);
            Map[x][y] = Map[y][x] = t;
        }
        Dijkstra();
        if(cas) puts("");
        printf("Scenario #%d:\n", ++cas);
        printf("%d\n", d[N]);
    }
    return 0;
}

/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define rep(i,a,b)      for(int i = a;i <= b;i++)
#define rep0(i,b)       for(int i = 0;i < b;i++)
#define rep1(i,b)       for(int i = 1;i <= b;i++)
#define MP(a,b)         make_pair(a,b)
#define PB(a)           push_back(a)
#define fst             first
#define snd             second
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x)          cout<<x<<"  ";
/****************************>>>>SEPARATOR<<<<****************************/
const int maxn = 1000 + 5;
const int INF = 0x3f3f3f3f;
int N,M;
struct Edge
{
    int from, to, dist;
    Edge() {}
    Edge(int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist) {}
};
struct HeapNode
{
    int pos, d;
    HeapNode() {}
    HeapNode(int _pos, int _d) : pos(_pos), d(_d) {}
    bool operator < (const HeapNode & p) const
    {
        return d < p.d;
    }
};
struct Dijkstra
{
    int n, s, d[maxn];
    bool vis[maxn];
    vector<Edge> edges;
    vector<int> G[maxn];
    Dijkstra() {}
    Dijkstra(int _n, int _s) : n(_n), s(_s) {}
    void init(int _n, int _s)
    {
        this->n = _n, this->s = _s;
        edges.clear();
        rep1(i, n)
        {
            G[i].clear();
        }
    }
    void AddEdge(int u, int v, int t)
    {
        edges.PB(Edge(u, v, t));
        int m = edges.size();
        G[u].PB(m - 1);
    }
    void dijkstra()
    {
        memset(vis, false, sizeof(vis));
        memset(d,0,sizeof(d));
        d[s] = INF;
        priority_queue<HeapNode> Que;
        Que.push(HeapNode(s, 0));
        while(!Que.empty())
        {
            HeapNode now = Que.top();
            Que.pop();
            int u = now.pos;
            if(vis[u]) continue;
            vis[u] = true;

            for(int i = 0; i < G[u].size(); i++)
            {
                Edge& e = edges[G[u][i]];
                if(d[e.to] < min(d[u], e.dist))
                {
                    d[e.to] = min(d[u], e.dist);
                    Que.push(HeapNode(e.to,d[e.to]));
                }
            }
        }
    }
}dij;

int main()
{
  //  FIN;
    int T,cas = 0,x,y,t;
    for(scanf("%d",&T);T--;)
    {
        scanf("%d %d",&N,&M);
        dij.init(N,1);
        rep(i,1,M)
        {
            scanf("%d %d %d",&x,&y,&t);
            dij.AddEdge(x,y,t);
            dij.AddEdge(y,x,t);
        }
        dij.dijkstra();
        if(cas) puts("");
        printf("Scenario #%d:\n",++cas);
        printf("%d\n",dij.d[N]);
    }
    return 0;
}

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/acmore_xiong/article/details/47291061
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞