[codeforces1081D]Maximum Distance

版权声明:辛辛苦苦码字,你们转载的时候记得告诉我 https://blog.csdn.net/dxyinme/article/details/85078162

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: x 1 , x 2 , , x k x_1,x_2,…,x_k .
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 n n , m m and k ( 2 k n 1 0 5 , n 1 m 1 0 5 ) k (2≤k≤n≤10^5, n−1≤m≤10^5 ) — the number of vertices, the number of edges and the number of special vertices.

The second line contains k k distinct integers x 1 , x 2 , , x k ( 1 x i n ) x_1,x_2,…,x_k (1≤xi≤n) .

Each of the following m lines contains three integers u , v u, v and w ( 1 u , v n , 1 w 1 0 9 ) w (1≤u,v≤n,1≤w≤10^9) , denoting there is an edge between u and v of weight w. 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.

Output

The first and only line should contain kintegers. The i t h i-th integer is the distance between x i x_i 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 1 1 and 2 2 equals to $24 because one can walk through the edge of weight 2 2 connecting them. So the distance to the farthest node for both 1 1 and 2 2 equals to 2 2 .

In the second example, one can find that distance between 1 1 and 2 2 , distance between 1 1 and 3 3 are both 3 3 and the distance between 2 2 and 3 3 is 2 2 .

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

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

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

#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;
}

猜你喜欢

转载自blog.csdn.net/dxyinme/article/details/85078162