本地评测机能过,提交WA,玄一关

P3366 【模板】最小生成树

return2147483647 @ 2024-07-23 21:34:20

使用的Prim算法,在本地样例和第一个点都能过,但是交上去就WA

#include<bits/stdc++.h>
using namespace std;
int n,m,sum;
bool book[5010];
vector<int>e[5010];
vector<int>w[5010];
priority_queue<pair<int,int> >q;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int u,v,ww;
        scanf("%d%d%d",&u,&v,&ww);
        e[u].push_back(v);
        w[u].push_back(ww);
        e[v].push_back(u);
        w[v].push_back(ww);
    }
    book[1]=1;
    for(int i=0;i<e[1].size();i++)
    {
        q.push(make_pair(e[1][i],w[1][i]));
    }
    while(!q.empty())
    {
        int vv=q.top().first;
        int ww=q.top().second;
        if(book[vv]==1)
        {
            q.pop();
            continue;
        }
        book[vv]=1;
        for(int i=0;i<e[vv].size();i++)
        {
            q.push(make_pair(e[vv][i],w[vv][i]));
        }
        sum+=ww;
        q.pop();
    }
    cout<<sum;
    return 0;
}

P3366_1.in

5 18
2 4 276
3 3 435
3 4 608
2 4 860
1 2 318
1 3 547
5 4 419
2 5 98
1 5 460
5 3 399
3 5 240
3 2 733
3 3 903
4 2 909
5 2 206
3 4 810
2 1 115
2 3 419

P3366_1.out

729

by return2147483647 @ 2024-07-23 21:39:48

还没有处理orz

但是剩下的点还是WA干净了


by lunjiahao @ 2024-07-23 21:40:49

为啥你的 Prim 长得这么奇怪(?)


by return2147483647 @ 2024-07-23 21:44:12

@lunjiahao 瞎写的


by lunjiahao @ 2024-07-23 21:46:53

等等你好像没有重载运算符吧

priority_queue 默认为大根堆啊 @return2147483647


by return2147483647 @ 2024-07-23 21:52:25

@lunjiahao 但是在自己机子上过了


by return2147483647 @ 2024-07-23 21:52:48

@lunjiahao 已经AC了!谢谢Orz


by lunjiahao @ 2024-07-23 22:00:16

@return2147483647

哥们你应该不会食言吧()


by return2147483647 @ 2024-07-29 19:37:21

@lunjiahao 不好意思,当时太激动了

已经让小号也关注了

6天不上古的屑


|