POJ 1797 Heavy Transportation

Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 17984   Accepted: 4744

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个路口,有些路口之间有路,且这些路都有自己的载重量。且每两个路口之间最多只有一条路。问从1到n号路口,最大的载重量为多少。

这道题其实是单源最短路的变形,从一个路口到另一个路口,有一条路。如果可以经过第三个路口,那么就有第二条路。如果第二条路的载重量大于第一条路,那么就做一次松弛操作。

由于这道题目的数据比较小,用dijkstra 和 spfa 两种算法的差距不大。


 

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

#include<cmath>

#define maxn 1005

#define INF 10000000

using namespace std;

int n,map[maxn][maxn];

void init()

{

    int m,i,j;

    scanf("%d%d",&n,&m);

    for (i=1; i<=n; i++)

        for (j=1; j<=n; j++)

            map[i][j]=0;//一开始赋初值为0

    while(m--)

    {

        int c;

        scanf("%d%d%d",&i,&j,&c);

        map[i][j]=map[j][i]=c;

    }

}

int min(int a, int b)

{

    return (a<b?a:b);

}

void dijkstra()

{

    int dist[maxn]={0},visit[maxn]={0};

    int i,j,k;

    for (i=1; i<=n; i++)

        dist[i]=map[1][i];

    visit[1]=1;

    for (i=1; i<n; i++)

    {

        int t=0;

        for (j=1; j<=n; j++)

            if (!visit[j] && dist[j]>t)

            {

                k=j;

                t=dist[j];

            }

        visit[k]=1;

        for (j=1; j<=n; j++)

            if (!visit[j] && dist[j]<min(dist[k],map[k][j]))//主要是松弛部分要注意

                dist[j]=min(dist[k],map[k][j]);

    }

    cout<<dist[n]<<endl;

}

void spfa()

{

    int i,j,visit[maxn]={0},dist[maxn]={0};

    queue<int> q;

    visit[1]=1;

    q.push(1);

    dist[1]=INF;

    while(!q.empty())

    {

        int t=q.front();

        q.pop();

        visit[t]=0;

        for (i=1; i<=n; i++)

            if (dist[i]<(min(dist[t],map[t][i])))

            {

                dist[i]=min(dist[t],map[t][i]);

                if (!visit[i])

                {

                    visit[i]=1;

                    q.push(i);

                }

            }

    }

    cout<<dist[n]<<endl;

}

int main ()

{

    int t,i=1;

    cin>>t;

    while(t--)

    {

        init();

        printf("Scenario #%d:\n",i++);

        //dijkstra();

        spfa();

        cout<<endl;

    }

    return 0;

}


 

 

你可能感兴趣的:(port)