WA了9个点,求大佬在源代码上更改
查看原帖
WA了9个点,求大佬在源代码上更改
1040131
WangZhaoyun0215楼主2025/7/21 15:00
#include <bits/stdc++.h>

using namespace std;

int n,m,ans,c,f[5005];

struct edge
{
    int x,y,z;
}a[100005];

bool cmp(edge a,edge b)
{
    return a.z < b.z;
}

int find(int x)
{
    return f[x] == x ? x : f[x] = find(f[x]);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i++)
        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
    sort(a + 1,a + m + 1,cmp);
    for(int i = 1;i <= n;i++)
        f[i] = i;
    for(int i = 1;i <= n;i++)
    {
        int fx = find(a[i].x);
        int fy = find(a[i].y);
        if(fx != fy)
        {
            f[fx] = fy;
            c++;
            ans += a[i].z;
        }
        if(c == n - 1)
        {
            cout << ans;
            return 0;
        }
    }
    cout << "orz";
    return 0;
}
2025/7/21 15:00
加载中...