ZOJ 1203 Swordfish (经典MST ~ Kruscal)Boruvka算法

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=203

Description:

We all remember that in the movie Swordfish, Gabriel broke into the World Bank Investors Group in West Los Angeles, to rob $9.5 billion. And he needed Stanley, the
best hacker in the world, to help him break into the password protecting the bank system. Stanley‘s lovely daughter Holly was seized by Gabriel, so he had to work for him. But at the last moment, Stanley made some little trick in his hacker mission: he injected
a trojan horse in the bank system, so the money would jump from one account to another account every 60 seconds, and would continue jumping in the next 10 years. Only Stanley knew when and where to get the money. If Gabriel killed Stanley, he would never get
a single dollar. Stanley wanted Gabriel to release all these hostages and he would help him to find the money back.

You who has watched the movie know that Gabriel at last got the money by threatening to hang Ginger to death. Why not Gabriel go get the money himself? Because these money keep jumping, and these
accounts are scattered in different cities. In order to gather up these money Gabriel would need to build money transfering tunnels to connect all these cities. Surely it will be really expensive to construct such a transfering tunnel, so Gabriel wants to
find out the minimal total length of the tunnel required to connect all these cites. Now he asks you to write a computer program to find out the minimal length. Since Gabriel will get caught at the end of it anyway, so you can go ahead and write the program
without feeling guilty about helping a criminal.

Input:

The input contains several test cases. Each test case begins with a line contains only one integer N (0 <= N <=100), which indicates the number of cities you have to connect.
The next N lines each contains two real numbers X and Y(-10000 <= X,Y <= 10000), which are the citie‘s Cartesian coordinates (to make the problem simple, we can assume that we live in a flat world). The input is terminated by a case with N=0 and you must not
print any output for this case.

Output:

You need to help Gabriel calculate the minimal length of tunnel needed to connect all these cites. You can saftly assume that such a tunnel can be built directly from
one city to another. For each of the input cases, the output shall consist of two lines: the first line contains "Case #n:", where n is the case number (starting from 1); and the next line contains "The minimal distance is: d", where d is the minimal distance,
rounded to 2 decimal places. Output a blank line between two test cases.

Sample Input:

5
0 0
0 1
1 1
1 0
0.5 0.5
0

Sample Output:

Case #1:
The minimal distance is: 2.83

分析:

1. 对图中各顶点,将与其关联,具有最小权边的边选入MST,得到由MST子树构成的森林;

2. 在图中陆续选择可以连接两颗不同子树且具有最小权值的边,将子树合并,最终构造成MST;

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <cmath>
#define MAXN 105  //顶点个数的最大值;
#define MAXM 5005  //边的个数的最大值;
#include <algorithm>
#define INF 0x1f1f1f1f
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

struct Edge
{
    int u, v;   //边的顶点;
    double w;   //边的权值;
}edge[MAXM];

int parent[MAXN], n, m, cas = 1;  //每个顶点对应的父亲节点, 顶点个数,边个数,测试数据个数;
double X[MAXN], Y[MAXN], res;     //每个顶点的X坐标和Y坐标;生成树的权值;

double Mul(double x) { return (double)x*x; }

void Init()
{
    for(int i=0; i<n; i++) scanf("%lf %lf", &X[i], &Y[i]);   //读入顶点个数;
    int k = 0;
    for(int i=0; i<n; i++) {
        for(int j=i+1; j<n; j++) {
            double dist = sqrt(Mul(X[i]-X[j]) + Mul(Y[i]-Y[j]));
            edge[k].u = i, edge[k].v = j, edge[k++].w = dist;
        }
    }
    m = k;
}

void Make_Set()    //初始化每个节点;
{
    for(int i=0; i<n; i++) {
        parent[i] = -1;
    }
}

int Find(int x)    //查找父亲节点;
{
    int s;
    for(s=x; parent[s]>=0; s=parent[s]);
    while(s != x) {
        int temp = parent[x];
        parent[x] = s;
        x = temp;
    }
    return s;
}

void Union_Set(int x, int y)   //节点合并;
{
    x = Find(x), y = Find(y);
    int temp = parent[x] + parent[y];
    if(parent[x] > parent[y]) {
        parent[x] = y;
        parent[y] = temp;
    }else {
        parent[y] = x;
        parent[x] = temp;
    }
}

int cmp(const void *a, const void *b)  //按边从小到大进行排序;
{
    Edge p1 = *(const Edge *)a;
    Edge p2 = *(const Edge *)b;
    if(p1.w > p2.w) return 1;
    else return -1;
}

void Kruscal()    //Kruscal 算法;
{
    int cnt = 0, Mc, Md;
    Make_Set();
    for(int i=0; i<m; i++) {
        Mc = edge[i].u, Md = edge[i].v;
        if(Find(Mc) != Find(Md)) {
            res += edge[i].w, cnt++;
            Union_Set(Mc, Md);
        }
        if(cnt >= n-1) break;
    }
}

int main()
{
    while(~scanf("%d", &n) && n) {
        Init();
        qsort(edge, m, sizeof(Edge), cmp);
        res = 0.0;
        Kruscal();
        if(cas > 1) printf("\n");
        printf("Case #%d:\n", cas++);
        printf("The minimal distance is: %.2lf\n", res);
    }
    return 0;
}

ZOJ 1203 Swordfish (经典MST ~ Kruscal)Boruvka算法

时间: 2024-10-01 03:25:14

ZOJ 1203 Swordfish (经典MST ~ Kruscal)Boruvka算法的相关文章

ZOJ 1203 Swordfish 剑鱼行动 最小生成树,Kruskal算法

题目链接:ZOJ 1203 Swordfish 剑鱼行动 Swordfish Time Limit: 2 Seconds      Memory Limit: 65536 KB There exists a world within our world A world beneath what we call cyberspace. A world protected by firewalls, passwords and the most advanced security systems.

ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 Seconds      Memory Limit: 65536 KB There exists a world within our world A world beneath what we call cyberspace. A world protected by firewalls, password

zoj 1203 Swordfish

链接:zoj 1203 题意:输入n个城市的坐标,输出使n个城市连通的最短路线的长度 分析:通过坐标可以将两两之间的长度即权值算出,再用最小生成树的算法 不过这个题要注意输出时的格式问题,两组数据间要空一行 #include<cstdio> #include<cmath> #include<algorithm> using namespace std; int f[110],n,m; struct stu { int a,b; double c; }t[5000]; i

zoj 1203 Swordfish (kruskal 克鲁斯卡尔)

Swordfish Time Limit: 2 Seconds      Memory Limit: 65536 KB There exists a world within our world A world beneath what we call cyberspace. A world protected by firewalls, passwords and the most advanced security systems. In this world we hide our dee

zoj 1203 Swordfish 【最小生成树 prim 算法】

Swordfish Time Limit: 2 Seconds      Memory Limit: 65536 KB There exists a world within our world A world beneath what we call cyberspace. A world protected by firewalls, passwords and the most advanced security systems. In this world we hide our dee

ZOJ 1203 Swordfish(最小生成树 kruskal)

题意  给你n个点的坐标  每个点都可与其它n-1个点相连  求这n个点的最小生成树的权重 裸的最小生成树  直接kruskal咯 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N = 105, M = 10050; double x[N], y[N], ans; int n, m , par[N];

ZOJ 1586 QS Network (经典MST~Prim)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two n

ZOJ 2158 &amp;&amp; POJ 1789 Truck History (经典MST)

链接:http://poj.org/problem?id=1789 或  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1158 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for br

最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind

最小支撑树树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind 最小支撑树树 前几节中介绍的算法都是针对无权图的,本节将介绍带权图的最小支撑树(minimum spanning tree)算法.给定一个无向图G,并且它的每条边均权值,则MST是一个包括G的所有顶点及边的子集的图,这个子集保证图是连通的,并且子集中所有边的权值之和为所有子集中最小的. 本节中介绍三种算法求解图的最小生成树:Prim算法.Kruskal算法和Boruvk