P3366[模板]最小生成树

如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出 orz。

输入格式

第一行包含两个整数 N,M,表示该图共有 N 个结点和 M 条无向边。

接下来 M行每行包含三个整数 X_i,Y_i,Z_i,表示有一条长度为 Z_i的无向边连接结点 X_i,Y_i

输出格式

如果该图连通,则输出一个整数表示最小生成树的各边的长度之和。如果该图不连通则输出 orz。

输入输出样例

输入

4 5
1 2 2
1 3 2
1 4 3
2 3 4
3 4 3

输出

7

数据规模:

对于 100% 的数据:\(1\le N\le 5000 ,1\le M\le 2\times 10^5\)

prim

#include <bits/stdc++.h>
namespace FastIO {
    char buf[1 << 21], buf2[1 << 21], a[20], *p1 = buf, *p2 = buf, hh = '\n';
    int p, p3 = -1;

    void read() {}

    void print() {}

    inline int getc() {
        return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
    }

    inline void flush() {
        fwrite(buf2, 1, p3 + 1, stdout), p3 = -1;
    }

    template<typename T, typename... T2>
    inline void read(T &x, T2 &... oth) {
        int f = 0;
        x = 0;
        char ch = getc();
        while (!isdigit(ch)) {
            if (ch == '-')
                f = 1;
            ch = getc();
        }
        while (isdigit(ch)) {
            x = x * 10 + ch - 48;
            ch = getc();
        }
        x = f ? -x : x;
        read(oth...);
    }

    template<typename T, typename... T2>
    inline void print(T x, T2... oth) {
        if (p3 > 1 << 20)
            flush();
        if (x < 0)
            buf2[++p3] = 45, x = -x;
        do {
            a[++p] = x % 10 + 48;
        } while (x /= 10);
        do {
            buf2[++p3] = a[p];
        } while (--p);
        buf2[++p3] = hh;
        print(oth...);
    }
} // namespace FastIO
#define read FastIO::read
#define print FastIO::print
//======================================
using namespace std;
const int maxn=5e3+10;
const int inf=0x3f3f3f3f;
typedef long long ll;
int n,m,e[maxn][maxn],ans,vis[maxn],d[maxn];
int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
    //freopen("2.txt", "w", stdout);
#endif
    //======================================
    read(n, m);
    memset(e,0x3f,sizeof(e));
    for (int i = 1, u, v, w; i <= m; i++) {
        read(u, v, w);
        e[u][v] = min(e[u][v], w);
        e[v][u] = min(e[v][u], w);
    }
    memset(d, 0x3f, sizeof(d));
    for (int i = 1; i <= n; i++) {
        d[i] = e[1][i];
    }
    vis[1]=1;
    for (int i = 1; i <= n; i++) {
        int mi = inf, miv = -1;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && d[j] < mi) {
                mi = d[j];
                miv = j;
            }
        }
        if (miv == -1) break;
        vis[miv] = 1;
        ans += mi;
        //print(mi);
        for (int j = 1; j <= n; j++) {
            if (!vis[j]) {
                d[j] = min(d[j], e[miv][j]);
            }
        }
    }
    print(ans);
    //======================================
    FastIO::flush();
    return 0;
}

堆优化的prim

#include <bits/stdc++.h>
namespace FastIO {
    char buf[1 << 21], buf2[1 << 21], a[20], *p1 = buf, *p2 = buf, hh = '\n';
    int p, p3 = -1;

    void read() {}

    void print() {}

    inline int getc() {
        return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
    }

    inline void flush() {
        fwrite(buf2, 1, p3 + 1, stdout), p3 = -1;
    }

    template<typename T, typename... T2>
    inline void read(T &x, T2 &... oth) {
        int f = 0;
        x = 0;
        char ch = getc();
        while (!isdigit(ch)) {
            if (ch == '-')
                f = 1;
            ch = getc();
        }
        while (isdigit(ch)) {
            x = x * 10 + ch - 48;
            ch = getc();
        }
        x = f ? -x : x;
        read(oth...);
    }

    template<typename T, typename... T2>
    inline void print(T x, T2... oth) {
        if (p3 > 1 << 20)
            flush();
        if (x < 0)
            buf2[++p3] = 45, x = -x;
        do {
            a[++p] = x % 10 + 48;
        } while (x /= 10);
        do {
            buf2[++p3] = a[p];
        } while (--p);
        buf2[++p3] = hh;
        print(oth...);
    }
} // namespace FastIO
#define read FastIO::read
#define print FastIO::print
//======================================
using namespace std;
const int maxn=4e5+10;
const int inf=0x3f3f3f3f;
typedef long long ll;
int n,m,ans,vis[maxn],head[maxn];
priority_queue<pair<int,int> ,vector<pair<int,int> >,greater<pair<int,int> > >q;
struct node{
    int v,nxt,w;
}e[maxn];
int t;
void add(int u,int v,int w) {
    t++;
    e[t].v = v;
    e[t].w = w;
    e[t].nxt = head[u];
    head[u] = t;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
    //freopen("2.txt", "w", stdout);
#endif
    //======================================
    read(n, m);
    for (int i = 1, u, v, w; i <= m; i++) {
        read(u, v, w);
        add(u, v, w);
        add(v, u, w);
    }
    int cnt=0;
    q.push(make_pair(0, 1));
    while (!q.empty()) {
        int w = q.top().first, u = q.top().second;
        q.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        ans += w;
        cnt++;
        if (cnt>=n) break;
        for (int i = head[u]; i; i = e[i].nxt) {
            int v=e[i].v;
             if (!vis[v]) q.push(make_pair(e[i].w, v));
        }
    }
    print(ans);
    //======================================
    FastIO::flush();
    return 0;
}

Kruskal

#include <bits/stdc++.h>
namespace FastIO {
    char buf[1 << 21], buf2[1 << 21], a[20], *p1 = buf, *p2 = buf, hh = '\n';
    int p, p3 = -1;

    void read() {}

    void print() {}

    inline int getc() {
        return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
    }

    inline void flush() {
        fwrite(buf2, 1, p3 + 1, stdout), p3 = -1;
    }

    template<typename T, typename... T2>
    inline void read(T &x, T2 &... oth) {
        int f = 0;
        x = 0;
        char ch = getc();
        while (!isdigit(ch)) {
            if (ch == '-')
                f = 1;
            ch = getc();
        }
        while (isdigit(ch)) {
            x = x * 10 + ch - 48;
            ch = getc();
        }
        x = f ? -x : x;
        read(oth...);
    }

    template<typename T, typename... T2>
    inline void print(T x, T2... oth) {
        if (p3 > 1 << 20)
            flush();
        if (x < 0)
            buf2[++p3] = 45, x = -x;
        do {
            a[++p] = x % 10 + 48;
        } while (x /= 10);
        do {
            buf2[++p3] = a[p];
        } while (--p);
        buf2[++p3] = hh;
        print(oth...);
    }
} // namespace FastIO
#define read FastIO::read
#define print FastIO::print
//======================================
using namespace std;
const int maxn=2e5+10;
const int inf=0x3f3f3f3f;
typedef long long ll;
int n,m,ans,f[maxn];
struct node{
    int u,v,w;
}e[maxn];
int find(int x) {
    return x == f[x] ? x : f[x] = find(f[x]);
}
bool cmp(const node &a,const node &b) {
    return a.w < b.w;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
    //freopen("2.txt", "w", stdout);
#endif
    //======================================
    read(n, m);
    for (int i = 1; i <= m; i++) {
        read(e[i].u, e[i].v, e[i].w);
    }
    for (int i = 1; i <= n; i++) {
        f[i] = i;
    }
    sort(e + 1, e + m + 1, cmp);
    for (int i = 1; i <= m; i++) {
        int u = find(e[i].u), v = find(e[i].v);
        if (u == v) continue;
        ans += e[i].w;
        f[u] = v;
    }
    print(ans);
    //======================================
    FastIO::flush();
    return 0;
}
posted @ 2020-06-27 13:22  Snow_in_winer  阅读(135)  评论(0编辑  收藏  举报