最小生成树 Maximum Distance

解决一个图论问题,给定一个加权无向连通图,找到特殊顶点间最大距离。输入包括顶点数、边数、特殊顶点数及顶点、边权重,输出为特殊顶点间的最大距离。

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

D. Maximum Distance

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn vertices and mm weighted edges. There are kk special vertices: x1,x2,…,xkx1,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 nn, mm and kk (2≤k≤n≤1052≤k≤n≤105, n−1≤m≤105n−1≤m≤105) — the number of vertices, the number of edges and the number of special vertices.

The second line contains kk distinct integers x1,x2,…,xkx1,x2,…,xk (1≤xi≤n1≤xi≤n).

Each of the following mm lines contains three integers uu, vv and ww (1≤u,v≤n,1≤w≤1091≤u,v≤n,1≤w≤109), denoting there is an edge between uu and vv of weight ww. The given graph is undirected, so an edge (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.

 

 

#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、付费专栏及课程。

余额充值