我做的时候这还是道绿题,怎么现在就蓝了
题目链接
题意
已经十分清楚了
题解
由题目可知, 所经过的边越大, 初始宽度越大。 所以跑一遍kruskal
我们考虑设 表示 能 经过点 的最大宽度。 则对于一条边的 , ,若是从 到 , 。 由此可得: 。
但我们会发现对于处理出的 ,我们发现他 是个孤儿 没地方存,若转移到 , , 会发现极其难处理。
这里我们需要一个 重构树 的东东。由此就转换成了对一条边的两个点,我们只需要求出他们的祖先并进行以上运算,讲答案存入新建点的 数组中。然后恭喜你,成功水了一道蓝题!
以下是样例的建图
上代码~
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define INF 0x7fffffff
#define ll long long
using namespace std;
const int N=2e5;
int n,m,fa[N],cnt;
ll f[N],c[N];
struct edge{
int u,v,w;
}e[N];
const ll inf=0x7fffffff;
inline int read()
{
register int x=0,t=1;
register char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
t=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*t;
}
int father(int x){
if(fa[x]!=x)fa[x]=father(fa[x]);
return fa[x];
}
void ub(int x,int y){
int fx=father(x);
int fy=father(y);
if(fx!=fy)fa[fx]=fy;
}
bool cmp(edge x,edge y){
return x.w>y.w;
}
void init() {
n=read();
m=read();
for(int i=1;i<=n;i++)c[i]=read(),fa[i]=i,f[i]=INF;
for(int i=1;i<=m;i++)e[i].u=read(),e[i].v=read(),e[i].w=read();
sort(e+1,e+1+m,cmp);
}
void kruskal(){
cnt=n;
for(int i=1;i<=m;i++){
int u=father(e[i].u),v=father(e[i].v);
if(u!=v){
c[++cnt]=c[u]+c[v];
fa[u]=fa[v]=fa[cnt]=cnt;
f[cnt]=max(min(f[v],1ll*e[i].w)-c[u],min(f[u],1ll*e[i].w)-c[v]);
}
}
printf("%lld\n",f[cnt]>0?f[cnt]:-1);
}
signed main() {
init();
kruskal();
return 0;
}

