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,…,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 n, m and k(2≤k≤n≤105,n−1≤m≤105) — the number of vertices, the number of edges and the number of special vertices.
The second line contains k distinct integers x1,x2,…,xk(1≤xi≤n).
Each of the following m lines contains three integers u,v and w(1≤u,v≤n,1≤w≤109), denoting there is an edge between u and v of weight w. The given graph is undirected, so an edge(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−th integer is the distance between xi 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 and 2 equals to $24 because one can walk through the edge of weight 2 connecting them. So the distance to the farthest node for both 1 and 2 equals to 2.
In the second example, one can find that distance between 1 and 2, distance between 1 and 3 are both 3 and the distance between 2 and 3 is 2.
The graph may have multiple edges between and self-loops, as in the first example.
题意:
给一个n个点m条无向边的连通图,这n个点中有k个是特殊点,定义一条路径的长度是这条路径上最长的边的长度,两个点之间的距离是这两个点之间的所有路径的最短长度,图中可能有重边和自环。
对于每个特殊点xi来说,设xj是和xi距离最远的特殊点,对于每个xi求出这个最长距离。
题解:
我们可以发现答案一定是最小生成树上的一条边,而且所有xi的答案都相同(任何一个在这条边一侧的特殊点都可以选择在这条边的另一侧的特殊点作为离自己距离最远的特殊点),那么只有当选取的边的两侧都有特殊点的时候他才可以被算作答案。我们就可以在计算最小生成树合并点集的时候记录一下一个点集中是否有特殊点来判断这个边是否需要用于更新答案。最后把答案输出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;
}
继续阅读与本文标签相同的文章
阿里人工智能实验室?对,这个神秘机构即将登场
-
韩国公布500亿美元计划 大力发展电动和自动驾驶汽车
2026-05-18栏目: 教程
-
原来这样做可以提高自媒体短视频的播放量?
2026-05-18栏目: 教程
-
用了3年以上的iPhone手机,应该这样清理手机缓存,很实用
2026-05-18栏目: 教程
-
一文弄懂,锁的基本概念到Redis分布式锁实现
2026-05-18栏目: 教程
-
阿里云混合云备份如何还原虚拟机备份?
2026-05-18栏目: 教程
