比赛 NOIP2023模拟赛1 评测结果 AAAAAAAAAAAATATTTTTT
题目名称 最小差异值 最终得分 65
用户昵称 ┭┮﹏┭┮ 运行时间 8.585 s
代码语言 C++ 内存使用 4.29 MiB
提交时间 2023-11-13 09:06:03
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3+10,M = 1e4+10;
int n,m,ans;
struct made{
    int x,y,z;
    made(){}
    made(int xx,int yy,int zz):x(xx),y(yy),z(zz){}
    bool operator <(const made u)const{
        return z < u.z;
    }
}a[M<<1];
struct t{
    int ver,nx;
}e[M<<1];
int hd[N],tot;
void add(int x,int y){
    tot++;
    e[tot].ver = y,e[tot].nx = hd[x],hd[x] = tot;
}
bool v[N];
void dfs(int x){
    v[x] = 1;
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(v[y])continue;
        dfs(y);
    }
}
bool check(int x){
    for(int i = 1;i <= m;i++){
        memset(e,0,sizeof(e));
        memset(v,0,sizeof(v));
        memset(hd,0,sizeof(hd));tot = 0;
        for(int j = i;a[j].z - a[i].z <= x && j <= m;j++){
            add(a[j].x,a[j].y);
            add(a[j].y,a[j].x);//无向图建成有向图,警钟长鸣 
        }
        dfs(1);
        bool f = 1;
        for(int j = 1;j <= n;j++)if(!v[j])f = 0;
        if(f == 1)return 1;
    }
    return 0;
}
int main(){
    freopen("dvalue.in","r",stdin);
    freopen("dvalue.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        a[i] = made(x,y,z);
    }
    sort(a+1,a+1+m);
//    for(int i = 1;i <= m;i++)cout<<a[i].z<<' ';
//    cout<<endl;
    int l = 0,r = 5e4;
    while(l < r){//二分答案 
        int mid = l + r >> 1;
        if(check(mid))r = mid;
        else l = mid+1;
        //cout<<mid<<endl;
    }
    printf("%d\n",l);
    
    return 0;
    
}