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.

In this world we hide

our deepest secrets,

our most incriminating information,

and of course, a shole lot of money.

This is the world of Swordfish.

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

题意:给定平面上N个城市的位置,计算连接这N个城市所需线路长度总和的最小值。

分析:最小生成树,Kruskal问题求解。注意两个城市之间都有一条边相连。还有每两组输出之间空一行。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

#define maxn 5055
double ans;
int cnt;
int parent[110];
struct edge
{
    int u, v;
    double w;
}EG[maxn];

bool cmp(edge a, edge b)
{
    return a.w < b.w;
}
int cmp2(const void *a, const void *b)
{
    edge aa = *(const edge*)a;
    edge bb = *(const edge*)b;
    if(aa.w > bb.w) return 1;
    return -1;
}
int Find(int x)
{
    if(parent[x] == -1) return x;
    return Find(parent[x]);
}
void Kruskal()
{
    memset(parent, -1, sizeof(parent));
    sort(EG, EG+cnt, cmp);
    //qsort(EG, cnt, sizeof(EG[0]), cmp2);
    ans = 0;
    for(int i = 0; i < cnt; i++)
    {
        int t1 = Find(EG[i].u), t2 = Find(EG[i].v);
        if(t1 != t2)
        {
            ans += EG[i].w;
            parent[t1] = t2;
        }
    }
}
int main()
{
    int n, cas = 0;
    double x[110], y[110];
    while(scanf("%d", &n), n)
    {
        for(int i = 0; i < n; i++)
            scanf("%lf%lf", &x[i], &y[i]);
        cnt = 0;
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++)
            {
                EG[cnt].u = i;
                EG[cnt].v = j;
                EG[cnt].w = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                cnt++;
            }
        Kruskal();
        if(cas > 0)
            printf("\n");
        printf("Case #%d:\nThe minimal distance is: %.2f\n", ++cas, ans);
        //++cas;
    }
    return 0;
}
时间: 2024-11-08 08:00:49

ZOJ 1203 Swordfish 剑鱼行动 最小生成树,Kruskal算法的相关文章

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 (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 (经典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 ha

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

SOJ4339 Driving Range 最小生成树 kruskal算法

典型的最小生成树 然后求最大的一条边 附上链接 http://cstest.scu.edu.cn/soj/problem.action?id=4339 需要注意的是有可能有 "IMPOSSIBLE" 的情况 这里用一个flag标记 记录所并的节点 只有flag = n时才能成功 负责就 "IMPOSSIBLE" 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring&g

最小生成树 kruskal算法简介

生成树--在一个图中的一个联通子图  使得所有的节点都被(访问) 最小生成树 (MST) 即联通子图的总代价(路程)最小 已知的一个图 有n个点 m条边 kruskal的算法如下 先对边从小到大排序 从最小的边起,不停的合并这条边的两个节点到一个集合,如果这条边的两个节点已经在一个集合里,则无视,否则形成回路(显然错误)直到所有的节点并到一个集合里 这里需要用到并查集来合并节点 1 int cmp(const int i,const int j) { 2 return w[i] < w[j];

最小生成树 Kruskal算法

Kruskal算法 1.概览 Kruskal算法是一种用来寻找最小生成树的算法,由Joseph Kruskal在1956年发表.用来解决同样问题的还有Prim算法和Boruvka算法等.三种算法都是贪婪算法的应用.和Boruvka算法不同的地方是,Kruskal算法在图中存在相同权值的边时也有效. 2.算法简单描述 1).记Graph中有v个顶点,e个边 2).新建图Graphnew,Graphnew中拥有原图中相同的e个顶点,但没有边 3).将原图Graph中所有e个边按权值从小到大排序 4)

无向图最小生成树Kruskal算法

问题 最小生成树的Kruskal算法 描述:有A.B.C.D四个点,每两个点之间的距离(无方向)是(第一个数字是两点之间距离,后面两个字母代表两个点):(1,'A','B'),(5,'A','C'),(3,'A','D'),(4,'B','C'),(2,'B','D'),(1,'C','D') 生成边长和最小的树,也就是找出一种连接方法,将各点连接起来,并且各点之间的距离和最小. 思路说明: Kruskal算法是经典的无向图最小生成树解决方法.此处列举两种python的实现方法.这两种方法均参考

最小生成树Kruskal算法的提出者Joseph Bernard Kruskal,Jr.

熟悉算法中的最小生成树的朋友都晓得有一个Kruskal算法,这个算法就是由题目中那个名字很长的人提出的.因为他功绩卓越,尊称他为Kruskal. Kruskal生于1928年1月29日,卒于2010年9月19日,美国人,维基里的词条中包含的头衔是:数学家.统计学家.计算机科学家.心理测量学专家. kruskal分别就读过芝加哥大学和普林斯顿大学,1954年获得博士学位. 下面链接中是一篇几年他的文章,从中可以了解他的更多成就.顺便说一句,他的父母和兄弟也都很牛. http://pan.baidu