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 k integers. The i-th integer is the distance between xi and the farthest special vertex from it.
Examples
inputCopy
2 3 2
2 1
1 2 3
1 2 2
2 2 1
outputCopy
2 2
inputCopy
4 5 3
1 2 3
1 2 5
4 2 1
2 3 2
1 4 4
1 3 3
outputCopy
3 3 3
Note
In the first example, the distance between vertex 1 and 2 equals to 2 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.
题意:
给你一张图,有自环,有重边,给你k个特殊点,然后规定,一个点到另一个点的距离是这条路上边权的最大值,问你每个特殊点到别的特殊点最小距离的最大值是多少。
题解:
刚开始还傻乎乎的dfs搜,写到后面感觉根本就不是1800的水平,然后就写不动了。。后来看别人博客才知道是并查集,因为这种最短路构成了一棵树,因为它并不是边权的相加而是最大值,所以每个点到别的点的最短路的最大值应该是一样的,假设有一个点到别的点的最大值比较小,那么别的点也会走这个比较小的权值的边而不是较大权值的边,那么就是一个最小生成树的特殊点中间的最大的边。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct node
{
int x,y,w;
bool operator< (const node& a)const
{
return w<a.w;
}
}e[N*2];
int spe[N],fa[N];
int finds(int x)
{
return x==fa[x]?x:fa[x]=finds(fa[x]);
}
int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
fa[i]=i;
int x;
for(int i=1;i<=k;i++)
{
scanf("%d",&x);
spe[x]=1;
}
for(int i=1;i<=m;i++)
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
sort(e+1,e+1+m);
int ans=0;
for(int i=1;i<=m;i++)
{
int x=finds(e[i].x);
int y=finds(e[i].y);
if(x!=y)
{
if(spe[x]&&spe[y])
ans=max(ans,e[i].w);
else if(spe[x]||spe[y])
spe[x]=spe[y]=1;
fa[y]=x;
}
}
for(int i=1;i<=k;i++)
printf("%d%c",ans,i==k?'\n':' ');
return 0;
}