玄关求调

P3366 【模板】最小生成树

ALLcry @ 2023-12-16 19:39:31

#include<bits/stdc++.h>
using namespace std;
const int inf=2e9+1;
struct edge{
    int from,ve;
}ans[5005];
bool red[5005];
vector<edge>a[5005];
int n,m;
void prim(int start){
    memset(red,0,sizeof(red));
    red[start]=1;
    for(int i=1;i<=n;i++)ans[i]=(edge){start,inf};
    ans[start].ve=0;
    for(int i=0;i<a[start].size();i++)ans[a[start][i].from]=(edge)
        {start,a[start][i].ve};
    for(int i=1;i<=n;i++){
        int sett,minn=inf;
        for(int j=1;j<=n;j++)
            if(red[j]==0&&ans[j].ve<minn)
                {minn=ans[j].ve;sett=j;}
        red[sett]=1;
        for(int j=0;j<a[sett].size();j++)
            if(red[a[sett][j].from]==0&&ans[a[sett][j].from].ve>a[sett][j].ve)
                ans[a[sett][j].from]=(edge){sett,a[sett][j].ve};
    }
}
int main(void){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int u,v,va;
        cin>>u>>v>>va;
        a[u].push_back((edge){v,va});
        a[v].push_back((edge){u,va});
    }
    prim(1);
    long long num=0;
    for(int i=1;i<=n;i++){
        if(ans[i].ve==inf){cout<<"orz";return 0;}
        num+=ans[i].ve;
    }
    cout<<num;
}

by ALLcry @ 2023-12-17 23:15:31

4、5两点 wa了,86 分,只与答案差了一点点啊!!(TAT) 好奇一下有没有跟我一样的


by ALLcry @ 2023-12-17 23:45:36

是3,4两点


|