POJ-1797Heavy 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.
题意:有n个编号依次为1 – n的城市和m条道路,然后又m行数据,表示两个城市之间的编号和道路能够承受的最大重量,让你求从1号城市运送到n号城市每次能运送的最重货物。
思路:本题只要用一个结构体把读取的两个编号和他们之间能承受的最大重量记录下来,然后再把结构体数组按照最大承重量排序就可以,然后再从头开始枚举,用一个min来记录最小的最大承重量,中间用并查集来记录这些城市之间是否连接,当1和n被连接后就跳出枚举就可以
完整代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX 1011
using namespace std;
const int INF = 33333333;
int pre[MAX];
struct node{
    int x,y;
    int l;
}s[MAX*100];
bool cmp(struct node a,struct node b)
{
    if(a.l > b.l)
        return 1;
    return 0;
}
int find(int x)//寻找根节点 
{
    int r = x;
    while(pre[r] != r)
        r = pre[r];
    int i = x,j;
    while(pre[i] != r)//压缩路径 
    {
        j = pre[i];
        pre[i] = r;
        i = j;
    }
    return r;
}
bool add(int x,int y)//连接两个编号城市 
{
    int fx = find(x);
    int fy = find(y);
    if(fx != fy)
    {
        pre[fx] = fy;
        return 1;//成功连接 
    }
    return 0;
}
int main(void)
{
    int n,m,a,b,c,t;
    scanf("%d",&t);
    int i,j,k;
    for(k=1;k<=t;k++)
    {
        int min = INF;
        int head = 0,tail = 0;
        scanf("%d %d",&n,&m);
        for(i=1;i<=n;i++)
            pre[i] = i;
        for(i=1;i<=m;i++)
        {
            scanf("%d %d %d",&a,&b,&c);//读入两个城市的编号,和其之间的最大承重量。 
            s[tail].x = a;
            s[tail].y = b;
            s[tail].l = c;
            tail++;
        }
        sort(s,s+tail,cmp);
        for(i=0;i<tail;i++)
        {
            int f = add(s[i].x,s[i].y);
            if(f == 1)
            {
                if(s[i].l < min)
                    min = s[i].l;
            }
            if(find(1) == find(n))//当1和n被连接后就跳出枚举。 
                break;
        }
        printf("Scenario #%d:\n%d\n\n",k,min);//注意两组数据之间要换行,所以要有两个换行符。 
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值