(玄关)58分求助

P3366 【模板】最小生成树

mairuisheng @ 2024-09-01 16:40:55

Result:58分

#include<cstdio>
#include<algorithm>
int n,s;
int ans,cnt;
int f[5001];
struct network
{
    int dx;
    int dy;
    int dis;
}nmap[5001];
bool cmp(network x,network y)
{
    return x.dis<y.dis;
}
int search(int x)
{
    if(f[x]!=x)f[x]=search(f[x]);
    return f[x];
}
int main()
{
    int i;
    scanf("%d %d",&n,&s);
    for(i=1;i<=s;++i)scanf("%d %d %d",&nmap[i].dx,&nmap[i].dy,&nmap[i].dis);
    for(i=1;i<=n;++i)f[i]=i;
    std::sort(nmap+1,nmap+1+s,cmp);
    for(i=1;i<=s;++i)
    {
        int r1=search(nmap[i].dx),r2=search(nmap[i].dy);
        if(r1!=r2)
        {
            f[r2]=r1;
            ans+=nmap[i].dis;
            cnt++;
        }
    }
    if(cnt==(n-1))
    {
        printf("%d",ans);
    }
    else puts("orz");
    return 0;
}

求助大佬


by EricWan @ 2024-09-01 16:46:59

@mairuisheng 一眼 nmap 开小了,给我关注


by sutiancheng @ 2024-09-01 16:48:36

nmap数组开小了


by mairuisheng @ 2024-09-01 16:49:25

谢谢,已关


by sutiancheng @ 2024-09-01 16:49:45

@mairuisheng


|