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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章