prim 93 pts 求调

P3366 【模板】最小生成树

XSS_Worm @ 2024-02-16 15:08:42

WA on #3

#include <bits/stdc++.h>
#define pii pair<int, int>
#define array(x, j)             \
    for (int i = 0; i < j; i++) \
        cout << x[i] << ' ';
#define ln cout << endl;
#define rpp(i, n) for (int i = 1; i <= n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define bk(i, st, ed) for (int i = st; i >= ed; i--)
#define pb push_back
#define pf push_front
#define iter iterator
#define fi first
#define se second
#define ll long long
using namespace std;
inline int read()
{
    int f = 1, s = 0;
    char c = getchar();
    for (; (c < '0' || c > '9') && c != '-'; c = getchar())
        ;
    if (c == '-')
    {
        f = -1;
        c = getchar();
    }
    for (; c >= '0' && c <= '9'; c = getchar())
        s = (s << 1) + (s << 3) + c - 48;
    return s * f;
}
void FastIO()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
template <class Tp>
inline void input(Tp &x) { cin >> x; }
template <class Tp, class... Oth>
inline void input(Tp &x, Oth &...y) { input(x), input(y...); }
template <class Tp>
inline void print(Tp x) { cout << x << ' '; };
template <class Tp, class... Oth>
inline void print(Tp x, Oth... y) { print(x), print(y...), cout << endl; }
const int N = 5005;
struct edge
{
    int to, val;
};
vector<edge> g[N];
set<int> a, b;
bool vis[N];
int cost[N], n, m;
void dfs(int cur)
{
    vis[cur] = 1;
    for (auto i : g[cur])
    {

        if (!vis[i.to])
        {

            dfs(i.to);
        }
    }
}
void prim()
{
    int ans = 0;
    memset(cost, 0x3f, sizeof(cost));
    a.insert(1);
    for (int i = 2; i <= n; i++)
    {
        b.insert(i);
    }
    for (auto i : g[1])
    {
        cost[i.to] = i.val;
    }
    while (b.size())
    {
        int cur = min_element(cost, cost + n + 1) - cost;
        int cst = cost[cur];
        ans += cst;
        cost[cur] = 0x3f3f3f3f;
        b.erase(cur);
        a.insert(cur);
        for (auto i : g[cur])
        {
            if (b.find(i.to) != b.end())
            {
                cost[i.to] = min(cost[i.to], i.val);
            }
        }
    }
    cout << ans << endl;
}
int main()
{
    input(n, m);
    rpp(i, m)
    {
        int u, v, w;
        input(u, v, w);
        g[u].pb({v, w});
        g[v].pb({u, w});
    }
    dfs(1);
    rpp(i, n)
    {
        if (!vis[i])
        {

            cout << "orz" << endl;
            return 0;
        }
    }
    prim();
    return 0;
}

by zjpwdyf @ 2024-02-16 15:18:43

蓝勾大佬还敲板题?


by zzk2010 @ 2024-02-16 15:22:16

会有重边吧

Prim初始化cost改成

for (auto i : g[1])
{
    cost[i.to] = min(cost[i.to], i.val);
}

试试


by zzk2010 @ 2024-02-16 15:22:47

蓝勾大佬还敲板题?


by XSS_Worm @ 2024-02-19 13:56:35

@zjpwdyf 我的蓝钩是通过 CSP-S 2023打暴力得来的


by XSS_Worm @ 2024-02-19 13:56:47

@zzk2010 谢谢 orz orz


by XSS_Worm @ 2024-02-19 13:57:30

@zjpwdyf 不代表我的水平(我的真实水平应该是5级绿钩)


by zjpwdyf @ 2024-02-19 18:05:15

@hanbangze 嘤嘤嘤,我 S 暴力写挂了,得了 135 分,痛失蓝勾


by jianhe @ 2024-02-22 11:24:12

同分。。@zjpwdyf


|