Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 25357 Accepted: 6717
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
这道题与前面的那道Frogger类似,相当于求图中点1到点n的一条路径使得这条路径中的最小边权最大。
要注意的一点是一定不要用cin来输入,会超时!
首先我用了dijikstra算法,把松弛条件改了一下,最后跑出来是469ms。
//
// main.cpp
// 最短路练习-C-Heavy Transportation
//
// Created by 袁子涵 on 15/10/9.
// Copyright (c) 2015年 袁子涵. All rights reserved.
//
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define maxof(a,b) ((a)>(b)?(a):(b))
#define minof(a,b) ((a)>(b)?(b):(a))
using namespace std;
int t,n;
long long int m,map[1005][1005],dis[1005],Max,out;
bool book[1005];
void dijkstra()
{
int next=0;
book[0]=1;
for (int i=0; i<n; i++) {
dis[i]=map[0][i];
}
for (int i=0; i<n; i++) {
Max=0;
for (int j=0; j<n; j++) {
if (dis[j]>=Max && book[j]==0) {
Max=dis[j];
next=j;
}
}
book[next]=1;
for (int j=0; j<n; j++) {
if (dis[j]<minof(map[next][j], dis[next])) {
dis[j]=minof(map[next][j], dis[next]);
}
}
}
}
int main(int argc, const char * argv[]) {
scanf("%d",&t);
int a,b;
long long int c;
int k=t;
while (k--) {
scanf("%d%lld",&n,&m);
memset(map, 0, sizeof(map));
memset(book, 0, sizeof(book));
memset(dis, 0, sizeof(dis));
for (int i=0; i<m; i++) {
scanf("%d%d%lld",&a,&b,&c);
map[a-1][b-1]=c;
map[b-1][a-1]=c;
}
dijkstra();
printf("Scenario #%d:\n",t-k);
printf("%lld\n\n",dis[n-1]);
}
return 0;
}
然后我用了prim算法,每次找最大的边,直到把n点加入树为之,理论上会稍微减小一点计算量,最后跑出来是422ms。
//
// main.cpp
// 最短路练习-C-Heavy Transportation-Prim
//
// Created by 袁子涵 on 15/10/9.
// Copyright (c) 2015年 袁子涵. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define minof(a,b) ((a)>(b)?(b):(a))
#define N 999999999
using namespace std;
int n,t;
long long int m,map[1005][1005],dis[1005],Max,out=N;
bool book[1005];
void prim()
{
int next=0;
book[0]=1;
for (int i=0; i<n; i++) {
dis[i]=map[0][i];
}
while (book[n-1]==0) {
Max=0;
for (int i=0; i<n; i++) {
if (Max<=dis[i] && book[i]==0) {
Max=dis[i];
next=i;
}
}
book[next]=1;
out=minof(out,Max);
for (int i=0; i<n; i++) {
if (dis[i]<map[next][i]) {
dis[i]=map[next][i];
}
}
}
}
int main(int argc, const char * argv[]) {
scanf("%d",&t);
int k=t,a,b;
long long int c;
while (k--) {
out=N;
memset(book, 0, sizeof(book));
memset(map, 0, sizeof(map));
scanf("%d%lld",&n,&m);
for (int i=0; i<m; i++) {
scanf("%d%d%lld",&a,&b,&c);
map[a-1][b-1]=c;
map[b-1][a-1]=c;
}
prim();
printf("Scenario #%d:\n",t-k);
printf("%lld\n\n",out);
}
return 0;
}