poj 1979 Heavy Transportation(spfa的应用)

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

Sample Output

 

题目大意:求出1-n所有路线中:每条路的最小边的最大权值。

其实这就是spfa的变种而已,我们只需要把以前处理的松弛操作改变一下就可以了:设置dis数组,dis[i]代表所有能到达i这个点的最小边的最大权值。now代表我们遍历到的一个当前节点,假设dis[now]已经求出。那么我们只需要判断一下dis[now]和now->next这条边的长度,如果len>dis[now]那么,我们便可以把dis[now]直接和dis[next]进行比较,确定从起点经now到next的最小边的最大权值,进行更新。如果len<dis[now]的话,那便意味着len一定是一条到now再到next的路径中的最小边。那么取len和dis[next]比较即可,进行更新。那么代码就很简单了:dis[next]=max(dis[now],min(dis[now],len))。

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注