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];
int cmp(struct stu x,struct stu y)
{
    return x.c<y.c;
}
int find(int x)
{
    if(x!=f[x])
        f[x]=find(f[x]);
    return f[x];
}
double krus()
{
    int i,k=0,x,y;
    double s;
    for(i=1;i<m;i++){
        x=find(t[i].a);
        y=find(t[i].b);
        if(x!=y){
            s+=t[i].c;
            k++;
            if(k==n-1)
                break;
            f[x]=y;
        }
    }
    return s;
}
int main()
{
    int i,j,k=0;
    double s,x[110],y[110];
    while(scanf("%d",&n)!=EOF){
        if(n==0)
            break;
        if(k>=1)
            printf("\n");
        k++;
        for(i=1;i<=n;i++){
            scanf("%lf%lf",&x[i],&y[i]);
            f[i]=i;
        }
        m=1;
        for(i=1;i<=n;i++)
            for(j=1;j<i;j++){
                t[m].a=i;
                t[m].b=j;
                t[m++].c=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));       //计算两两间的距离
            }
        sort(t+1,t+m,cmp);
        s=krus();
        printf("Case #%d:\nThe minimal distance is: %.2lf\n",k,s);
    }
    return 0;
}

zoj 1203 Swordfish,布布扣,bubuko.com

时间: 2024-10-12 11:24:54

zoj 1203 Swordfish的相关文章

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 (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 【最小生成树 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题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an