Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 40609 | Accepted: 10661 |
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.
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
給你一個圖,任意兩個節點直接有橋樑相連,每個橋樑有一個最大承重,問你若要從 1 號 到 n 號卡車的最大載重是多少。
最早接觸這題是朗訊杯
當時是二分答案 + BFS
然後之前做的一題給了我靈感
二份答案 + 並查集
先把所有邊排序,然後二分
判斷是否可達
剛開始從小到大排序 WA 飛了
然後這題 m 的範圍沒給
RE 飛了
體驗極差
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#define pb push_back
#define mp make_pair
#define LL long long
using namespace std;
///重邊 自環
const int N = 100010;
struct Node
{
int u;
int v;
int w;
};
bool cmp(Node a, Node b)
{
return a.w > b.w;
}
int n, m;
int ne[N];
Node node[N];
void ini()
{
for(int i = 1; i <= n; i ++){
ne[i] = i;
}
}
int Find(int x)
{
int t = x;
while(t != ne[t]){
t = ne[t];
}
while(t != x){
int q;
q = ne[x];
ne[x] = t;
x = q;
}
return t;
}
void Join(int x, int y)
{
x = Find(x);
y = Find(y);
if(x != y){
ne[x] = y;
}
}
int main()
{
int ncase;
int nc = 1;
int ans;
int l;
int r;
scanf("%d", &ncase);
while(ncase --){
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i ++){
scanf("%d%d%d", &node[i].u, &node[i].v, &node[i].w);
}
sort(node, node + m, cmp);
l = 0;
r = m - 1;
while(l <= r){
int mid = l + ((r - l) >> 1);
ini();
for(int i = 0; i <= mid; i ++){
Join(node[i].u, node[i].v);
}
if(Find(1) == Find(n)){
ans = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
printf("Scenario #%d:\n", nc ++);
printf("%d\n\n", node[ans].w);
}
return 0;
}
上了讀入掛以後達到了 79MS
眉開眼笑