UPC-2784 Model Railroad(最小生成树)

探讨如何在固定长度的轨道下,重新布局模型火车轨道,确保每个车站都能相互连通。通过对现有连接进行优化,拆除部分长轨道并替换为更有效的连接,实现整个网络的连通性。

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

题目描述
Since childhood you have been fascinated by model railroads. Designing your own tracks,complicated intersections, train stations with miniatures of travellers, train operators, luggage is so much fun! However, it also needs a lot of space. Since your house is more than full by now, you decide to move to the garden.
You have already moved all your completed tracks outside when you notice an important flaw:
Since different tracks were in different rooms before, there are stations which cannot be reached from each other. That has to change!
Since you have already fixed the exact positions of the stations, you know the lengths for all possible connections you can build and also which stations are connected already. All connections can be used in both directions. You can decide to remove some existing connections and instead build new ones of at most the same total length. Is it possible to rearrange the railroads so that every station is reachable from all other stations?

输入
The input consists of:
• one line with three integers n (2 ≤ n ≤ 5 · 104 ), m (0 ≤ m ≤ 2.5 · 105 ) and l (0 ≤ l ≤ m),where n is the number of stations, m is the number of possible connections and l is the number of connections already built;
• m lines describing the connections. Each connection is described by:
– one line with three integers a, b (1 ≤ a, b ≤ n), and c (0 ≤ c ≤ 5 · 103 ) describing that there is a connection from station a to station b of length c.
The first l of those connections already exist.

输出
Output “possible” if it is possible to construct a connected network as described above,otherwise output “impossible”.

样例输入
4 6 3
1 2 1
2 1 2
3 4 2
1 3 2
1 4 3
2 4 2

样例输出
possible

提示
Figure E.1 depicts the first sample case. It is possible to connect all stations by removing the connections between stations 1 and 2 of length 2 and instead building the connection between stations 2 and 4. The curvature of the rails does not matter because you have a hammer. In the second case, depicted in Figure E.2, it is not possible to connect all three stations.

题意: 有n个节点,m条边,其中前L条边是已经存在的,已经存在的L条边能被等长度的一些边替换,即删除一些已存在边,可以新建出m条边中一些等长的边。问是否能用已有长度联通所有的N个节点。
题解: 因为给出了前L条边是已经存在的,首先就对其求和。所以拆掉一些较长边,新建一些较短边,只要使其联通就好了,而不是用较短边通过组合代替较长边。

可能他给的前I条就是不联通的,但是前I条如果够长,还是能新建删除来联通的,如果一开始给的I条边的sum就特别小,那联通的边长一定会超sum,就是impossible.

然后因为是所有点都联通,那么直接求一颗最小生成树。然后对比生成树长度是否小于或等于前L条边总长度即可得到答案。

#include<bits/stdc++.h>
#define LL long long
#define M(a,b) memset(a,b,sizeof a)
#define pb(x) push_back(x)
using namespace std;
const int maxn=5e4+7;
struct edge
{
    int from,to;
    LL val;
    edge(){}
    edge(int a,int b,LL c)
    {
        from=a,to=b,val=c;
    }
    bool operator <(const edge &a)const
    {
        return val<a.val;
    }
}mp[maxn*10];
int n,m,l,z[maxn];
int finds(int x)
{
    return z[x]==x?x:z[x]=finds(z[x]);
}
void join(int x,int y)
{
    z[finds(x)]=y;
}
int main()
{
    scanf("%d%d%d",&n,&m,&l);
    for(int i=1;i<=n;i++)z[i]=i;
    LL sum=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&mp[i].from,&mp[i].to,&mp[i].val);
        sum+=i<=l?mp[i].val:0;
    }
    sort(mp+1,mp+1+m);
    int cnt=0;
    LL dist=0;
    for(int i=1;i<=m;i++)
    {
        if(finds(mp[i].from)!=finds(mp[i].to))
        {
            join(mp[i].from,mp[i].to);
            dist+=mp[i].val;
            cnt++;
        }
    }
    printf("%s\n",cnt==n-1&&dist<=sum?"possible":"impossible");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值