[codeforces1081D]Maximum Distance

这是一个关于图论的问题,给定一个包含n个节点、m条加权边的连通无向图,其中k个节点是特殊节点。定义路径成本为路径上最大权重的边,距离为两个节点间最小成本路径。对于每个特殊节点,找出另一个与之距离最远的特殊节点,并输出两者之间的距离。题目要求在提高限制后找到解决方案。

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

time limit per test :1 second
memory limit per test : 256 megabytes

Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago.
You are given a connected undirected graph with n
vertices and m weighted edges. There are k special vertices: x1,x2,…,xkx_1,x_2,…,x_kx1,x2,,xk.
Let’s define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them.

For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them.

The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him.

Input

The first line contains three integers nnn, mmm and k(2≤k≤n≤105,n−1≤m≤105)k (2≤k≤n≤10^5, n−1≤m≤10^5 )k(2kn105,n1m105) — the number of vertices, the number of edges and the number of special vertices.

The second line contains kkk distinct integers x1,x2,…,xk(1≤xi≤n)x_1,x_2,…,x_k (1≤xi≤n)x1,x2,,xk(1xin).

Each of the following m lines contains three integers u,vu, vu,v and w(1≤u,v≤n,1≤w≤109)w (1≤u,v≤n,1≤w≤10^9)w(1u,vn,1w109), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge(u,v)(u,v)(u,v)can be used in the both directions.
The graph may have multiple edges and self-loops.
It is guaranteed, that the graph is connected.

Output

The first and only line should contain kintegers. The i−thi-thith integer is the distance between xix_ixi and the farthest special vertex from it.

Examples
Input

2 3 2
2 1
1 2 3
1 2 2
2 2 1

Output

2 2 

Input

4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3

Output

3 3 3 

Note

In the first example, the distance between vertex 111 and 222 equals to $24 because one can walk through the edge of weight 222 connecting them. So the distance to the farthest node for both 111 and 222 equals to 222.

In the second example, one can find that distance between 111 and 222, distance between 111 and 333 are both 333 and the distance between 222 and 333 is 222.

The graph may have multiple edges between and self-loops, as in the first example.

题意:
给一个nnn个点mmm条无向边的连通图,这nnn个点中有kkk个是特殊点,定义一条路径的长度是这条路径上最长的边的长度,两个点之间的距离是这两个点之间的所有路径的最短长度,图中可能有重边和自环。
对于每个特殊点xix_ixi来说,设xjx_jxj是和xix_ixi距离最远的特殊点,对于每个xix_ixi求出这个最长距离。

题解:
我们可以发现答案一定是最小生成树上的一条边,而且所有xix_ixi的答案都相同(任何一个在这条边一侧的特殊点都可以选择在这条边的另一侧的特殊点作为离自己距离最远的特殊点),那么只有当选取的边的两侧都有特殊点的时候他才可以被算作答案。我们就可以在计算最小生成树合并点集的时候记录一下一个点集中是否有特殊点来判断这个边是否需要用于更新答案。最后把答案输出kkk次就行了。

#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
struct edge{
    int u,v,w;
}e[100004];
int f[100004],n,m,k;
int sp[100004];
inline bool dex(edge A,edge B){return A.w<B.w;}
int Find(int x){return (f[x]==x)?x:f[x]=Find(f[x]);}
int w33ha(){
    memset(sp,0,sizeof(sp));
    for(int i=1;i<=n;i++)f[i]=i;
    int x;
    for(int i=1;i<=k;i++){
        scanf("%d",&x);
        sp[x]=1;
    }
    for(int i=1;i<=m;i++)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    sort(e+1,e+m+1,dex);
    int ans=0;
    for(int i=1;i<=m;i++){
        int p=Find(e[i].u),q=Find(e[i].v);
        if(p!=q){
            if(sp[p]&&sp[q])ans=max(ans,e[i].w);
            else if(sp[p]||sp[q]){
                sp[p]=1;
                sp[q]=1;
            }
            f[p]=q;
        }
    }
    for(int i=1;i<k;i++)printf("%d ",ans);
    printf("%d\n",ans);
    return 0;
}
int LiangJiaJun(){
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)w33ha();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值