POJ 1797-Heavy Transportation(最短路变形)

xiaoxiao2021-02-28  26

Heavy Transportation Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 45397 Accepted: 11858

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的路使得这条路中的最小承重最大。

解题思路:运用dijstra算法求解,对最短路进行变形即可,我们用数组dis[i]存从1到点i的最大承重值。具体算法看代码吧!

AC代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include<string> #include<math.h> #include<stdlib.h> #include<queue> #include<map> #include<set> #include<stack> #define bug printf("*********\n"); #define mem0(a) memset(a, 0, sizeof(a)); #define mem1(a) memset(a, -1, sizeof(a)); #define finf(a, n) fill(a, a+n, INF); #define in1(a) scanf("%d" ,&a); #define in2(a, b) scanf("%d%d", &a, &b); #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c); #define out1(a) printf("%d\n", a); #define out2(a, b) printf("%d %d\n", a, b); #define pb(G, a, b) G[a].push_back(b); using namespace std; typedef long long LL; typedef pair<int, int> par; const int mod = 1e9+7; const int INF = 1e9+7; const int N = 1000010; const double pi = 3.1415926; int n, m, cnt; int head[1010], dis[1010], vis[1010]; struct edge { int to; int next; int cost; }e[1010*1010]; void add(int u, int v, int w) { e[cnt].to = v; e[cnt].next = head[u]; e[cnt].cost = w; head[u] = cnt ++; } void dijstra() { mem0(dis); //初始化dis的值为0 priority_queue<pair<int, int> > q; q.push(make_pair(INF ,1)); //记得这里初始化为INF,或者初始化dis[1] = INF也行 while(!q.empty()) { par cur = q.top(); q.pop(); int a = cur.first; int b = cur.second; for(int i = head[b]; i != -1; i = e[i].next) { int en = e[i].to; if(dis[en] < e[i].cost && dis[en] < a) { //走到该节点的前一段路的承重一定要大于该点的承重 dis[en] = min(a, e[i].cost); //更新 q.push(make_pair(dis[en] ,en)); } } } } int main() { int x, y, z, T, t = 1; scanf("%d", &T); while(T --) { scanf("%d%d", &n, &m); mem1(head); mem0(dis); mem0(vis); cnt = 0; for(int i = 0; i < m; i ++) { scanf("%d%d%d", &x, &y, &z); add(x, y, z); add(y, x, z); } dijstra(); printf("Scenario #%d:\n", t ++); printf("%d\n\n", dis[n]); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2400092.html

最新回复(0)