报错了,救救孩子

P3366 【模板】最小生成树

slipknot @ 2022-07-26 15:05:11

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int n,m,sum=0;
int f[114514];
struct huh{
    int u,v,w;
};
vector<huh> a;
int find(int v)
{
    if(f[v]==v)
    {
        return v;
    }
    else
    {
        f[v]=find(f[v]);
        return v;
    }
}

void hebing(int u,int v)
{
    int t1,t2;
    t1=find(u);
    t2=find(v);
    if(t1!=t2)
    {
        f[v]=t1;
    }
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        a.push_back(huh{x,y,z});
    }
    sort(a.front().w,a.back().w);
    for(int i=1;i<=n;i++)
    {
        f[i]=i;
    }
    for(int i=0;i<m;i++)
    {
        if(find(a[i].u)!=find(a[i].v))
        {
            hebing(a[i].u,a[i].v);
            sum+=a[i].w;
        }
        else continue;
    }
    cout<<sum;

    return 0;
}

by slipknot @ 2022-07-26 15:07:11

第42行“ a.push_back(huh{x,y,z});” 报错” [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]“


by bamboo12345 @ 2022-07-26 15:07:21

@slipknot sort应该没有这种写法吧


by slipknot @ 2022-07-26 15:09:53

@bamboo123 不大明白,sort不是(排序开始,排序结束)吗


by dlydly @ 2022-07-26 15:11:12

手写cmp


by RainSpark @ 2022-07-26 15:11:47

@slipknot sort 没有这种写法,只能写 sort(a.begin(),a.end());


by slipknot @ 2022-07-26 15:11:48

@bamboo123 我好像明白了


by slipknot @ 2022-07-26 15:12:12

@takeoff37808 感谢大佬


by RainSpark @ 2022-07-26 15:12:22

@slipknot h=或者是手写cmp函数


|