克鲁斯卡尔70pts RE on#11 #12#13 求调,玄关
  • 板块P1194 买礼物
  • 楼主DPOI
  • 当前回复9
  • 已保存回复9
  • 发布时间2025/7/30 18:32
  • 上次更新2025/7/31 09:25:31
查看原帖
克鲁斯卡尔70pts RE on#11 #12#13 求调,玄关
1690315
DPOI楼主2025/7/30 18:32
#include<bits/stdc++.h>
using namespace std;
struct edge{
    int st,to,nxt,w;
}e[5005];
int hd[505],tot;
int f[505];
void add(int u,int v,int w){
    e[++tot].to=v;
    e[tot].st=u;
    e[tot].nxt=hd[u];
    e[tot].w=w;
    hd[u]=tot;
}
bool cmp(edge x,edge y){
    return x.w<y.w;
}
int find(int x){
    if (f[x]==x)return x;
    return f[x]=find(f[x]);
}
void join(int u,int v){
    f[find(u)]=find(v);
}
int main(){
    int a,n;cin>>a>>n;
    for (int i=1;i<=n;++i){
        for (int j=1;j<=n;++j){
            int x;cin>>x;
            if (x==0)x=a;
            add(i,j,x);
        }
    }
    for (int i=1;i<=n;++i)f[i]=i;
    sort(e+1,e+tot+1,cmp);
    int ans=0,cnt=0;
    for (int i=1;i<=tot;++i){
        int u=e[i].st,v=e[i].to,w=e[i].w;
        if (cnt==n-1)break;
        if (find(u)==find(v))continue;
        join(u,v);
        ans+=min(w,a),cnt++;
    }
    cout<<ans+a;
}
2025/7/30 18:32
加载中...