POJ 1797 Heavy Transportation(单源最短路径变形)

Time Limit: 3000MS    Memory Limit: 30000K

描述

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.

输入

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.

输出

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.

样例输入

1 3 3
1 2 3
1 3 4
2 3 5

样例输出

Scenario #1:
4

题意

这道题就是一道图论题,n个点m个有权边的图,要你求从1出发,最后到达n时经过的路径中最小边的最大可能权值。可以理解为求单源最短路径的变形,将Dijkstra算法进行一个简单的变形就可以了,每次记录到达点k的最小边权值,若新的路径最小边权值比原有的最小边权值大,则更新路径。初始化时需要邻接矩阵或邻接表置为负数而不是INF。另外输出的时候需要注意每个case后要输出一个空行。这道题没有卡O(n^2)的算法,所以不需要用堆或优先队列来优化Dijkstra算法也能过。这里把两种Dijkstra算法的解都写出来。

普通Dijkstra算法变形 O(n^2)

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;

int t,n,m;
int link[1005][1005],dis[1005],visit[1005];

void dijkstra(int st)
{
    visit[st]=1;
    for(int i=1;i<=n;i++){
        if(link[st][i]>=0){
            dis[i]=link[st][i];
        }
    }
    dis[1]=INF;
    for(int i=2;i<=n;i++){
        int mind=-1,u=st;
        for(int j=1;j<=n;j++){
            if(!visit[j]&&dis[j]>mind){
                u=j;
                mind=dis[j];
            }
        }
        visit[u]=1;
        for(int j=1;j<=n;j++){
            if(!visit[j]&&link[u][j]>-1){
                if(dis[j]<min(link[u][j],dis[u])){
                    dis[j]=min(link[u][j],dis[u]);
                }
            }
        }
    }
}

int main()
{
    int a,b,c;
    scanf("%d",&t);
    for(int k=1;k<=t;k++){
        memset(link,-1,sizeof(link));
        memset(visit,0,sizeof(visit));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            dis[i]=-1;
        }
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&c);
            link[a][b]=c;
            link[b][a]=c;
        }
        dijkstra(1);
        printf("Scenario #%d:\n",k);
        printf("%d\n\n",dis[n]);
    }
    return 0;
}

Dijkstra算法+优先队列优化 O(nlogn)

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;

struct Node{
    int u,c;
    Node(int _u=0,int _c=0):u(_u),c(_c){}
    bool operator<(const Node &x)const{
        return c<x.c;
    }
};

struct Edge{
    int v,w;
    Edge(int _v,int _w):v(_v),w(_w){}
};
int t,n,m,dis[1005];
bool visit[1005];
vector<Edge> link[1005];

void dijkstra(int st)
{
    priority_queue<Node> q;
    while(!q.empty())   q.pop();
    int v,w;
    for(int i=0;i<link[st].size();i++){
        v=link[st][i].v;
        w=link[st][i].w;
        dis[v]=w;
        q.push(Node(v,w));
    }
    dis[1]=INF;
    visit[st]=true;
    Node tmp;
    while(!q.empty()){
        tmp=q.top();
        q.pop();
        if(visit[tmp.u])  continue;
        visit[tmp.u]=true;
        for(int j=0;j<link[tmp.u].size();j++){
            v=link[tmp.u][j].v;
            w=link[tmp.u][j].w;
            if(!visit[v]&&dis[v]<min(dis[tmp.u],w)){
                dis[v]=min(dis[tmp.u],w);
                q.push(Node(v,dis[v]));
            }
        }
    }
}

int main()
{
    int a,b,c;
    scanf("%d",&t);
    for(int k=1;k<=t;k++){
        memset(visit,0,sizeof(visit));
        memset(dis,-1,sizeof(dis));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            link[i].clear();
        }
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&c);
            link[a].push_back(Edge(b,c));
            link[b].push_back(Edge(a,c));
        }
        dijkstra(1);
        printf("Scenario #%d:\n",k);
        printf("%d\n\n",dis[n]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值