POJ - 1797 Heavy Transportation

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个 城市M条边(路),每条路都有个最大载重量,题目要你找一条运输路线,使得从1到N的运输量最大。

思路

    其实就是在求最短路的前提下载重量要最大。有那么一点动态规划的意思。

代码



#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int MAX = 1000 + 5; const int INF = 987654321; int dist[MAX], a[MAX][MAX];//dist存的其实不是路径而是载重量,a存的是图 int n = 0, m = 0; bool book[MAX]; void Dijkstra() { memset(book, false, sizeof(book[0])*(n + 5)); for (int i = 1; i <= n; i++) dist[i] = a[1][i]; dist[1] = INF; book[1] = true; for (int i = 1; i < n; i++) { int t = 1, temp = 0; for (int j = 1; j <= n; j++) { if (!book[j] && temp < dist[j]) { t = j; temp = dist[j]; } } book[t] = true; for (int j = 1; j <= n; j++) { if (!book[j]) dist[j] = max(dist[j], min(dist[t], a[t][j]));//关键代码,最短路的同时取载重量最大的边继续拓展 } } } int main() { ios::sync_with_stdio(false); int t = 0, kase = 0, u = 0, v = 0, w = 0; cin >> t; while (t--) { cin >> n >> m; for (int i = 1; i <= n; i++) memset(a[i], 0, sizeof(a[i][0]) * (n + 5)); for (int i = 0; i < m; i++) { cin >> u >> v >> w; a[u][v] = a[v][u] = max(a[u][v], w);//处理重边 } Dijkstra(); cout << "Scenario #" << ++kase << ":" << endl; cout << dist[n] << endl << endl; } return 0; } //
全部评论

相关推荐

07-02 13:52
武汉大学 golang
骗你的不露头也秒
牛客87776816...:😃查看图片
点赞 评论 收藏
分享
05-13 02:01
已编辑
惠州学院 前端工程师
安静的少年在求佛:建议把公司名字写到标题。以后有人想搜就能直接搜到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务