poj-1251 hdu-1301、poj-1287、poj-2421、zoj-1586、poj-1789、poj-1258、hdu-1233、hdu-1875最小生成树kruskal模板题集合

题意: N个顶点的无向图,给你每条边的长度,要你求该图的最小生成树.其中每个点用大写字母A-Z表示.

转换一下输入的格式就好了

链接:poj 1251  && hdu 1301

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 1000 + 10;
const int maxm = 1000000 + 10;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int n, m;

struct node {
    int a, b, c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxn];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

int kruskal(int n, int m) {
    sort(q + 1, q + 1 + m);
    int ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    if(nedge < n - 1) ans = -1;
    return ans;
}

int main()
{
    int T, kcase = 0;
    while(scanf("%d", &n) && n) {
        char ch;
        int k;
        int a;
        m = 0;
        init();
        for(int i = 1; i < n; i++) {
            cin >> ch >> k;
            char ch1;
            while(k--) {
                cin >> ch1 >> a;
                q[++m].a = ch - 'A' + 1;
                q[m].b = ch1 - 'A' + 1;
                q[m].c = a;
            }
        }
        int ans = kruskal(n, m);
        printf("%d\n", ans);
    }
    return 0;
}

题意:给出n个节点,再有m条边,这m条边代表从a节点到b节点电缆的长度,现在要你将所有节点都连起来,并且使长度最小

裸题。。。。。。

链接:poj 1287

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 10000 + 10;
const int maxm = 1000000 + 10;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int n, m;

struct node {
    int a, b;
    int c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxn];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

int kruskal(int n, int m) {
    init();
    sort(q + 1, q + 1 + m);
    int ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    if(nedge < n - 1) ans = -1;
    return ans;
}

int main()
{
    int T, kcase = 0;
    while(scanf("%d", &n) && n) {
        scanf("%d", &m);
        for(int i = 1; i <= m; i++) {
           scanf("%d %d %d", &q[i].a, &q[i].b, &q[i].c);
        }
        int ans = kruskal(n, m);
        printf("%d\n", ans);
    }
    return 0;
}

题意:首行给出N,接着是个N*N的矩阵,map[i][j]就代表i到j的权值。接着给出Q,下面Q行,每行两个数字A,B,代表A到B已经有边。最后输出最小生成树的权值和就行

有边的设为0即可

链接:poj 2421

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 10000 + 10;
const int maxm = 1000000 + 10;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int n, m;

struct node {
    int a, b;
    int c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxn];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

int kruskal(int n, int m) {
    init();
    sort(q + 1, q + 1 + m);
    int ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    if(nedge < n - 1) ans = -1;
    return ans;
}

int gp[maxn][maxn];

int main()
{
    int T, kcase = 0;
    while(scanf("%d", &n) != EOF) {
        int c;
        m = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                scanf("%d", &gp[i][j]);
            }
        }
        int k;
        scanf("%d", &k);
        int a, b;
        while(k--) {
            scanf("%d %d", &a, &b);
            //a++, b++;
            gp[a][b] = gp[b][a] = 0;
        }
        for(int i = 1; i <= n; i++) {
            for(int j = i + 1; j <= n; j++) {
                q[++m].a = i;
                q[m].b = j;
                q[m].c = gp[i][j];
            }
        }
        int ans = kruskal(n, m);
        printf("%d\n", ans);
    }
    return 0;
}

题意:在一个叫做QS的星系,他们使用一些特殊的通讯方式,两个人之间通讯需要使用一个网络适配器,但是一个网络适配器只能跟一个人联系,所有它连接几个人就需要几个适配器,而且每个人都有一些不同的偏好,喜欢的适配器的牌子也是不同的,现在让你求出来让QS人之间相互通讯最少需要多少代价?
输入第一行是测试数据组数,下面是一个N,代表N个人,下一样有N个数表示每个人喜欢的适配器的价格,接着一个矩阵,表示两点间通讯电缆的价格。

在边权上加上适配器价格即可

链接:zoj 1586

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 1000 + 10;
const int maxm = 1000000;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int n, m;

struct node {
    int a, b;
    int c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxm];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

int kruskal() {
    init();
    sort(q + 1, q + 1 + m);
    int ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    if(nedge < n - 1) ans = -1;
    return ans;
}

int gp[maxn][maxn];
int price[maxn];

int main()
{
    int T, kcase = 0;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &price[i]);
        }
        int c;
        m = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                scanf("%d", &gp[i][j]);
                if(j > i) {
                    q[++m].a = i;
                    q[m].b = j;
                    q[m].c = gp[i][j] + price[i] + price[j];
                }
            }
        }
        int ans = kruskal();
        printf("%d\n", ans);
    }
    return 0;
}

题意:给出n个长度相同的字符串,一个字符串代表一个点,每两个字符串有多少个字符不同,则不同的个数即为两点之间的距离,要求各个点都连通求quality的最大值

分母越小越好,转化为最小生成树问题

链接:poj 1789

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 2000 + 10;
const int maxm = 2000000;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int n, m;

struct node {
    int a, b;
    int c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxm];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

int kruskal() {
    init();
    sort(q + 1, q + 1 + m);
    int ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    if(nedge < n - 1) ans = -1;
    return ans;
}

int gp[maxn][maxn];
int price[maxn];
char str[maxn][105];

int main()
{
    int T, kcase = 0;
    while(~scanf("%d", &n) && n) {
        m = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%s", str[i]);
            for(int j = 1; j < i; j++) {
                int res = 0;
                for(int k = 0; k < strlen(str[i]); k++) {
                    if(str[j][k] != str[i][k]) res++;
                }
                q[++m].a = i;
                q[m].b = j;
                q[m].c = res;
            }
        }
        int ans = kruskal();
        printf("The highest possible quality is 1/%d.\n", ans);
    }
    return 0;
}

题意:有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离

直接写就行了。。。。。。

链接:poj 1258

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 2200 + 10;
const int maxm = 1001000;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int num[maxn];

int n, m;

struct node {
    int a, b;
    int c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxm];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

int kruskal() {
    init();
    sort(q + 1, q + 1 + m);
    int ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    //if(nedge < n - 1) ans = -1;
    return ans;
}

int main()
{
    int T, kcase = 0;
    while(~scanf("%d", &n)) {
        m = 0;
        int res;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                scanf("%d", &res);
                if(j > i) {
                    q[++m].a = i;
                    q[m].b = j;
                    q[m].c = res;
                }
            }
        }
        int ans = kruskal();
        printf("%d\n", ans);
    }
    return 0;
}

题意:中文题

直接kruskal

链接:hdu 1233

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 10000 + 10;
const int maxm = 1000000 + 10;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int n, m;

struct node {
    int a, b, c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxn];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

int kruskal(int n, int m) {
    sort(q + 1, q + 1 + m);
    int ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    if(nedge < n - 1) ans = -1;
    return ans;
}

int main()
{
    int T, kcase = 0;
    while(scanf("%d", &n) && n) {
        init();
        for(int i = 1; i <= n * (n - 1) / 2; i++) {
            scanf("%d %d %d", &q[i].a, &q[i].b, &q[i].c);
        }
        int ans = kruskal(n, n * (n - 1) / 2);
        printf("%d\n", ans);
    }
    return 0;
}

题意: 中文题

注意加边的限制条件,数据类型

链接:hdu 1875

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long

using namespace std;

const int maxn = 10000 + 10;
const int maxm = 1000000 + 10;
const int inf = 0x3f3f3f3f;

int fa[maxn];
int rnk[maxn];

int n, m;

struct node {
    int a, b;
    double c;
    friend bool operator < (node x, node y) {
        return x.c < y.c;
    }
}q[maxn];

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

int getf(int x) {
    if(x != fa[x]) {
        fa[x] = getf(fa[x]);
    }
    return fa[x];
}

void unions(int x, int y) {
    x = getf(x);
    y = getf(y);
    if(x == y) return;
    if(rnk[x] < rnk[y]) {
        fa[x] = y;
    }
    else {
        fa[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

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

double kruskal(int n, int m) {
    sort(q + 1, q + 1 + m);
    double ans = 0;
    int nedge = 0;
    for(int i = 1; i <= m && nedge != n - 1; i++) {
        if(getf(q[i].a) != getf(q[i].b)) {
            unions(q[i].a, q[i].b);
            ans += q[i].c;
            nedge++;
        }
    }
    if(nedge < n - 1) ans = -1;
    return ans;
}

int x[maxn], y[maxn];

double dis(int i, int j) {
    return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
}

int main()
{
    int T, kcase = 0;
    cin >> T;
    while(T--) {
        scanf("%d", &n);
        init();
        m = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d %d", &x[i], &y[i]);
            for(int j = 1; j < i; j++) {
                if(dis(i, j) >= 10 && dis(i, j) <= 1000) {
                    q[++m].a = i;
                    q[m].b = j;
                    q[m].c = 100 * dis(i, j);
                }
            }
        }
        double ans = kruskal(n, m);
        if(ans != -1)printf("%.1lf\n", ans);
        else puts("oh!");
    }
    return 0;
}





你可能感兴趣的:(最小生成树)