求助,Prim

P3366 【模板】最小生成树

LFYdemi @ 2024-08-24 14:26:37

Prim,样例都不对QwQ求条

#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

int n,m;
struct node{
    int v,w;
};
vector <node> g[10005];
int vis[10005],dis[10005];
int ans;

int main(){

    cin>>n>>m;
    for(int i=1,u,v,w;i<=m;i++){
        cin>>u>>v>>w;
        g[u].push_back((node){v,w});
        g[v].push_back((node){u,w});
    }
    memset(dis,0x3f,sizeof dis);
    int x=1,cnt=0;
    dis[1]=0;
    while(x){
        vis[x]=1;
        for(int i=0;i<g[x].size();i++){
            int v=g[x][i].v,w=g[x][i].w;
            if(dis[v]>w) dis[v]=w;
        }
        x=0;
        for(int i=1;i<=n;i++){
            if(!vis[i]&&dis[i]<0x3f3f3f3f){
                if(!x||dis[x]>dis[i]) x=i;
            }
        }
        if(x) cnt++;
        ans+=dis[x];
    }
    if(cnt<n-1) cout<<"orz";
    else cout<<ans;

    return 0;
}

by untitled_cpp @ 2024-08-24 14:30:04

@LFYdemi 把0x3f变成2147483647


by LFYdemi @ 2024-08-24 14:33:28

@untitled_cpp thx,此贴街~已棺


by szrgjxms @ 2024-08-24 14:34:14

vis[x]=1;
for(int i=0;i<g[x].size();i++){
    int v=g[x][i].v,w=g[x][i].w;
    if(dis[v]>w) dis[v]=w;
}

看不懂,想问问你这是什么意思


by untitled_cpp @ 2024-08-24 14:38:05

@LFYdemi 深金


|