poj1797-Heavy Transportation(最大生成树)

本文深入探讨了如何利用最大生成树算法解决运输路径中的重量限制问题,通过具体实例讲解了算法实现过程,包括数据结构定义、关键步骤及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Heavy Transportation

Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 50315 Accepted: 12988

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的所有路径中的最短边中的最大值。

这样就觉得好复杂。但是换一种想法,就是那我就找最长路,最长路上的最短边一定就是答案了。

一下就变成了最大生成树裸题了,注意的地方就是1和n一旦联通了就输出最后那条边就可以了。

也可以用dijkstra来做。

AC代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int maxn=1000010;
int cnt;
int fa[maxn],head[maxn];
struct node{
    int u;
    int v,next,val;
}G[maxn];
void init(){
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(fa,-1,sizeof(fa));
    ///memeset(G,0,sizeof(G));
}
void add(int a,int b,int c){
    G[++cnt].v=b;
    G[cnt].u=a;
    G[cnt].val=c;
    G[cnt].next=head[a];
    head[a]=cnt;
}
int findset(int x){return fa[x]==-1?x:fa[x]=findset(fa[x]);}
void join(int x,int y)
{
    int dx = findset(x), dy = findset(y);
    fa[dx] = dy;
}
int cmp(node a,node b){
    return a.val>b.val;
}
int main()
{
    int t;
    int n,m,a,b,c,ans;
    scanf("%d",&t);
    int cas=0;
    while(t--){
        cas++;
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        sort(G+1,G+1+m,cmp);
        ans=-1;
        for(int i=1;i<=m;i++){
            int u=G[i].u;
            int v=G[i].v;
            if(findset(u)!=findset(v)){
                join(u,v);
            }
            if(findset(1)==findset(n)){
                ans=G[i].val;
                break;
            }
        }
        printf("Scenario #%d:\n%d\n",cas,ans);
        printf("\n");

    }
    return 0;
}
/*
1
3 3
1 2 3
1 3 4
2 3 5

*/

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值