Featured image of post [模板]最小生成树

[模板]最小生成树

Kruskal

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

const int N = 200010, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m, f[N], cnt = 0;
ll ans = 0;
struct Node {
    int u, v;
    ll w;

    bool operator<(const Node b) const { return w < b.w; }
};
vector<Node> g;

void add(int u, int v, int w) { g.push_back({u, v, w}); }

void init() {
    for (int i = 1; i <= n; i++) f[i] = i;
}

int find(int x) {
    if (x != f[x]) f[x] = find(f[x]);
    return f[x];
}

void merge(int x, int y) { f[find(x)] = find(y); }

bool same(int x, int y) { return find(x) == find(y); }

void k() {
    for (int i = 0; i < m; i++) {
        if (same(g[i].u, g[i].v)) continue;
        merge(g[i].u, g[i].v);
        ans += g[i].w;
        cnt++;
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data/in.txt", "r", stdin);
    freopen("data/out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        add(u, v, w);
    }
    init();
    sort(g.begin(), g.end());
    k();
    if (cnt < n - 1)
        cout << "orz";
    else
        cout << ans;
    return 0;
}

Prim

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

const int N = 2e5 + 10, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m;
vi g[N];
vll w[N];
ll dis[N], ans;
bool vis[N];
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;

void addEdge(int u, int v, int ww) {
    g[u].push_back(v);
    g[v].push_back(u);
    w[u].push_back(ww);
    w[v].push_back(ww);
}

void prim() {
    memset(dis, 0x3f, sizeof(dis));
    dis[1] = 0;
    q.push({0, 1});
    while (!q.empty()) {
        pair<ll, int> p = q.top();
        q.pop();
        int u = p.second;
        ll ww = p.first;

        if (vis[u]) continue;
        vis[u] = true;
        ans += ww;

        for (int i = 0; i < g[u].size(); i++) {
            int v = g[u][i];
            if (dis[v] > w[u][i]) {
                dis[v] = w[u][i];
                q.push({dis[v], v});
            }
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data/in.txt", "r", stdin);
    freopen("data/out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        addEdge(u, v, w);
    }
    prim();
    for (int i = 1; i <= n; i++)
        if (!vis[i]) {
            cout << "orz";
            return 0;
        }
    cout << ans;
    return 0;
}
Licensed under CC BY-NC-SA 4.0
本站已安全运行
总访问量 次 | 访客 人 | 共 27 篇文章
Built with Hugo
主题 StackJimmy 设计