POJ 1797 Heavy Transportation

发布时间:2024-01-16 19:33:45
Heavy Transportation
Time Limit: 3000MS?Memory Limit: 30000K
Total Submissions: 15562?Accepted: 4044

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

Source

TUD Programming Contest 2004, Darmstadt, Germany
?
?
求1点到n点所有路径中权值最小边的最大值
?
类SPFA
?
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;

int n,m;
int edge_cnt;

int head[1005];
int next[1000005];
int point[1000005];
int weight[1000005];

int dis[1005];//dis表示1点到该点路径中最小权值的最大值
bool fl[1005];

void add_edge(int u,int v,int w)
{
    next[edge_cnt]=head[u];
    point[edge_cnt]=v;
    weight[edge_cnt]=w;
    head[u]=edge_cnt;
}

void fun()
{
    memset(dis,0,sizeof(dis));
    memset(fl,0,sizeof(fl));

    queue<int>q;

    q.push(1);
    fl[1]=1;
    dis[1]=0x1f1f1f1f;//源点要初始化成无穷,其他点初始化成0

    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        fl[u]=0;

        for(int e=head[u];e!=-1;e=next[e])
        {
            int v=point[e];
            if(weight[e]<dis[u])//如果该边小于u点的dis值,那么从u点到v点的所有路径中的最小权值的最大值就是该边的权值
            {
                if(weight[e]>dis[v])//如果此时该边权值大于v点原dis值,那么更新v点dis值为weight[e],否则v点dis值不变
                {
                    dis[v]=weight[e];
                    if(!fl[v])
                    {
                        q.push(v);
                    }
                }
            }
            else if(dis[v]<dis[u])//如果该边权值大于v点dis值,则v点dis值为dis[u]、dis[v]中大的一个
            {
                dis[v]=dis[u];
                if(!fl[v])
                {
                    q.push(v);
                }
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    int sen=1;
    while(t--)
    {
        memset(head,-1,sizeof(head));
        edge_cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            edge_cnt++;
            add_edge(u,v,w);
            edge_cnt++;
            add_edge(v,u,w);
        }
        fun();
        printf("Scenario #%d:\n%d\n\n",sen++,dis[n]);
    }
    return 0;
}

文章来源:https://blog.csdn.net/freedomcheng/article/details/7895331
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。