prim pt44求调

P3366 【模板】最小生成树

SugarKite @ 2023-10-20 19:59:58

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
int n,m,ans=0,cnt=0;
vector<PII>mp[50050];
int dis[50050],vis[50050];
priority_queue<PII,vector<PII>,greater<PII> >q;
signed main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int x,y,z;
        cin>>x>>y>>z;
        mp[x].push_back({z,y});
        mp[y].push_back({z,x});
    }
    for(int i=1;i<=n;i++){
        dis[i]=0x3f3f3f3f;
    }
    dis[1]=0;
    q.push({0,1});
    while(q.size()&&cnt<n){
        int id=q.top().second;
        int data=q.top().first;
        q.pop();
        if(vis[id])continue;
        vis[id]=1;
        cnt++;
        ans+=data;
        dis[id]=0;
        for(int i=0;i<mp[id].size();i++){
//          cout<<"try:"<<mp[id][i].second<<endl;
//          cout<<"test:"<<dis[mp[id][i].second]<<" "<<mp[id][i].first<<endl;
            if(dis[mp[id][i].second]>mp[id][i].first&&!vis[i]){
//              cout<<"push:"<<mp[id][i].first<<endl;
                dis[mp[id][i].second]=mp[id][i].first;
                q.push({dis[mp[id][i].second],mp[id][i].second});
            }
        }
    }
    cout<<cnt<<endl;
    if(cnt==n)cout<<ans<<endl;
    else cout<<"orz"<<endl;
    return 0;
} 

by SugarKite @ 2023-10-20 20:06:51

cout<<cnt<<endl;忘记删了,交的时候删了的


by wxw_zl @ 2023-10-20 20:09:28

if(dis[mp[id][i].second]>mp[id][i].first&&!vis[mp[id][i].second])
if(dis[mp[id][i].second]>mp[id][i].first&&!vis[i])

第三十五行,判读是否在已经更新的集合里面那个地方,应该是判断(mp[id][i].second)不是(i)

考前帮别人调代码,rp+=INF.


|