POJ 1797 kruskal 算法

题目链接:http://poj.org/problem?id=1797

开始题意理解错。不说题意了。

并不想做这个题,主要是想测试kruskal 模板和花式并查集的正确性。

已AC;

/*
最小生成树 kruskal算法
过程:每次选取没有参与构造最小生成树并且加入之后不会构成回路的边中权值最小的一条
作为最小生成树的一条新边。直至选择了V-1条边。
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define maxn 2100
using namespace std;

int fa[maxn], r[maxn];

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

int find_fa(int v) { // 递归式路径压缩
    if (fa[v] != v) fa[v] = find_fa(fa[v]);
    return fa[v];
}

/*int find_fa(int v) { // 非递归式路径压缩
    int k, j, r;
    r = v;
    while(r != fa[r])
        r = fa[r]; //找到根节点 记录在r上。
    k = v;
    while(k != r) {
        j = fa[k];
        fa[k] = r;
        k = j;
    }
    return r;
}*/

/*void unin(int u, int v) { // 非按秩合并
    int fau = find_fa(u);
    int fav = find_fa(v);
    if (fau != fav)
        fa[fav] = fau;
}*/

void unin(int u, int v) { // 按秩合并
    int fau = find_fa(u);
    int fav = find_fa(v);
    if (fau == fav) return;

    if (r[u] < r[v]) fa[fau] = fav;
    else {
        if (r[u] == r[v])
            r[u]++;
        fa[fav] = fau;
    }
}

struct Edge {
    int u, v, w;
}edge[1000010];

bool cmp(Edge a, Edge b) {
    return a.w > b.w;
}

int ans;
int kruskal(int n, int m) { // 传入顶点个数n 和 边的个数m
    init(n);
    sort(edge, edge+m, cmp);
    ans = 0;
    int ret = 0; // 生成树的总权值
    int cnt = 0; // 已加入最小生成树的边的数量

    for (int i=0; i<m; ++i) {
        if (find_fa(1) == find_fa(n)) return -1;
        int u = edge[i].u;
        int v = edge[i].v;
        if (find_fa(u) != find_fa(v)) {
            cnt++;
            ret += edge[i].w;
            unin(u, v);
            ans = edge[i].w;
        }
        if (cnt == n-1) return ret; // 已找到n-1条边,生成树构造完毕
    }
    return -1;
}

int main() {
    int casee = 1;
    int t;
    cin >> t;
    while(t--) {
        int n, m;
        cin >> n >> m;
        for (int i=0; i<m; ++i) {
            cin >> edge[i].u >> edge[i].v >> edge[i].w;
        }
        kruskal(n, m);
        cout << "Scenario #" << casee++ << ":" << endl << ans << endl << endl;
    }
    return 0;
}

  

时间: 2025-01-09 07:43:55

POJ 1797 kruskal 算法的相关文章

POJ 2485(Kruskal算法)

第一道Kruskal算法题 #include <iostream>#include <algorithm>#include <cstdio>using namespace std;#define max 505int f[max],maxw;struct edge{    int st,en,w;}ed[max*max/2]; int  find(int k){    if(k!=f[k])f[k]=find(f[k]);    return f[k];}void co

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

题目连接:ZOJ 1542 POJ 1861 Network 网络 Network Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, t

HDU 1301 &amp;POJ 1215 Jungle Roads【最小生成树,Prime算法+Kruskal算法】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6737    Accepted Submission(s): 4893 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

poj——2031 最小生成树(MST) Kruskal算法

poj——2031 最小生成树(MST)  Kruskal算法 Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4985   Accepted: 2503 Description You are a member of the space station engineering team, and are assigned a task in the constructio

poj 1789 Truck History(kruskal算法)

题目链接:http://poj.org/problem?id=1789 思路:把每一行看成一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列写的,但是超时啦,不知道为什么,希望有人可以解答.后面用的数组sort排序然后才AC. code: 数组sort排序AC代码: #include<cstdio> #include<queue> #include<algorithm> #include<io

POJ 3723 Conscription (最大权森林 + Kruskal算法)

Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8317   Accepted: 2887 Description Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be