Prim写的有点怪 但是过题了 求佬分析分析
查看原帖
Prim写的有点怪 但是过题了 求佬分析分析
877860
Just_Love_You楼主2023/8/24 19:41
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
constexpr int N=1e4+10;
constexpr int mod=1e9+7;
struct p
{
    int l;
    int r;
    int dis;
    bool operator <(const p& x) const{return x.dis<dis;}
}t;
bool v[N];
void solve()
{
    int n,m;
    priority_queue<p> pq;
    cin >> n >> m;
    std::vector<int> f[N];
    vector<int> ds[N];
    for(int i=1;i<=m;i++)
    {
        int l,r,d;
        cin >> l >> r >> d;
        f[l].emplace_back(r);
        f[r].emplace_back(l);
        ds[l].emplace_back(d);
        ds[r].emplace_back(d);
    }
    v[1]=true;
    for(auto i=f[1].begin();i!=f[1].end();i++)
    {
        int num=i-f[1].begin();
        pq.push(p{1,*i,*(ds[1].begin()+num)});
    }
    int ans=0;
    while(!pq.empty())
    {
        t=pq.top();
        pq.pop();
        if(!v[t.r])
        {
            ans+=t.dis;
            v[t.r]=true;
            for(auto i=f[t.r].begin();i!=f[t.r].end();i++)
            {
                int num=i-f[t.r].begin();
                pq.push(p{t.r,*i,*(ds[t.r].begin()+num)});
            }
        }
    }
    cout << ans << endl;
}
signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    // int o;
    // cin >> o;
    // while(o--)
    solve();
    return 0;
}
//
//⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
//⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
//⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
//⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
//⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
//⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
//⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇ 
//⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
//
2023/8/24 19:41
加载中...