http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203

大意:

给出一些点,求MST

把这几天的MST一口气发上来。

kruskal

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=101;
const int INF=9999999;
int fa[MAXN]; struct point
{
double x,y;
}ship[MAXN]; struct dot
{
int x,y;
double dis;
}dis[MAXN*MAXN]; int find(int cur)
{
return fa[cur]==cur?cur:fa[cur]=find(fa[cur]);
} bool operator < (const dot &a,const dot& b)
{
return a.dis<b.dis;
} int main()
{
int n,kase=1;
while(~scanf("%d",&n),n)
{
if(kase!=1)
printf("\n");
int i,j;
for(i=1;i<=n;i++)
scanf("%lf%lf",&ship[i].x,&ship[i].y); int len=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
dis[len].x=i;
dis[len].y=j;
dis[len].dis=sqrt((ship[j].y -ship[i].y) *(ship[j].y -ship[i].y) + (ship[j].x -ship[i].x)*(ship[j].x -ship[i].x));
len++;
}
} for(i=1;i<=n;i++)
fa[i]=i; sort(dis,dis+len);
double ans=0;
for(i=0;i<len;i++)
{
int rootx=find(dis[i].x);
int rooty=find(dis[i].y);
if(rootx!=rooty)
{
ans+=dis[i].dis;
fa[rootx]=rooty;
}
}
printf("Case #%d:\n",kase++);
printf("The minimal distance is: %.2lf\n",ans);
}
return 0; }

prim

#include<cstdio>
#include<cmath>
const int MAXN=101;
const int INF=9999999;
double map[MAXN][MAXN];
double dis[MAXN];
struct point
{
double x,y;
}ship[MAXN]; void prim(int n)
{
for(int i=1;i<=n;i++)
dis[i]=INF; bool vis[MAXN]={0}; int cur=1;
vis[cur]=1;
dis[cur]=0;
int i,j; for(i=1;i<=n;i++)
{
double mini=INF;
for(j=1;j<=n;j++)
if(!vis[j] && dis[j] > map[cur][j])
dis[j]=map[cur][j]; for(j=1;j<=n;j++)
if(!vis[j] && mini > dis[j])
mini=dis[cur=j]; vis[cur]=true;
}
}
int main()
{
int n,kase=1;
while(~scanf("%d",&n),n)
{
if(kase!=1)
printf("\n");
int i,j;
for(i=1;i<=n;i++)
scanf("%lf%lf",&ship[i].x,&ship[i].y); for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
map[i][j]= map[j][i] =
sqrt((ship[j].y -ship[i].y) *(ship[j].y -ship[i].y) + (ship[j].x -ship[i].x)*(ship[j].x -ship[i].x));
}
} prim(n);
double ans=0,ans2=0;//=map[a][b];
for(i=1;i<=n;i++)
ans+=dis[i];
printf("Case #%d:\n",kase++);
printf("The minimal distance is: %.2lf\n",ans);
}
}

ZOJ 1203 Swordfish MST的更多相关文章

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

    主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...

  2. ZOJ 1203 Swordfish(Prim算法求解MST)

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

  3. ZOJ 1203 Swordfish

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

  4. zoj 1203 Swordfish prim算法

    #include "stdio.h". #include <iostream> #include<math.h> using namespace std; ...

  5. POJ 1751 Highways (ZOJ 2048 ) MST

    http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...

  6. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  7. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  8. ZOJ 1914 Arctic Network (POJ 2349) MST

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 http://poj.org/problem?id=2349 题目大 ...

  9. ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST

    ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 POJhttp://poj.org/problem?id=23 ...

随机推荐

  1. C#引用c++DLL结构体数组注意事项(数据发送与接收时)

    本文转载自:http://blog.csdn.net/lhs198541/article/details/7593045 最近做的项目,需要在C# 中调用C++ 写的DLL,因为C# 默认的编码方式是 ...

  2. 通过OpenSSL解析X509证书基本项

    在之前的文章"通过OpenSSL解码X509证书文件"里.讲述了怎样使用OpenSSL将证书文件解码,得到证书上下文结构体X509的方法. 以下我们接着讲述怎样通过证书上下文结构体 ...

  3. Windows Embedded POSready2009

    Windows Embedded POSready2009 ,这个看上去和 XP 差不多,可能是别人说的 XPE 系统 下载 POSready2009_CD.iso, 安装 KEY :         ...

  4. js32---CommonUtil.js

    // BH 命名空间 namespace var BH = {} ; BH.Interface = function(name,methods){ //Interface是类.方法的名字,以后用BH. ...

  5. java初始化过程中成员变量

    package day01; class Base{ int j; //1.j=0 Base(){ add(1); //2.调用子类add()方法 System.out.println(j); //4 ...

  6. java移位操作符注意的问题

    如果对char,byte或者short类型的数值进行移位处理,那么在移位进行之前,他们会被转为int类型, 并且所得到的结果也是一个int型. 若对long类型的数值进行处理所得到的结果也是long. ...

  7. 9.Maven之(九)依赖关系

    转自:https://yq.aliyun.com/ziliao/312160 在maven的管理体系中,各个项目组成了一个复杂的关系网,但是每个项目都是平等的,是个没有贵贱高低,众生平等的世界,全球每 ...

  8. 在VS中设置比较和谐的字体和颜色的方法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 先在studiostyl.es网站选择你喜欢的字体方案,我个人比较喜欢这款: Humane Studio,注意在网页上 ...

  9. Hp Open View安装使用视频

    去年完成的cisco works 2000操作(http://chenguang.blog.51cto.com/blog/350944/124729)视频深受广大博友欢迎许多人来信咨询,最近刚做完一个 ...

  10. 洛谷——P3817 小A的糖果

    https://www.luogu.org/problem/show?pid=3817 题目描述 小A有N个糖果盒,第i个盒中有a[i]颗糖果. 小A每次可以从其中一盒糖果中吃掉一颗,他想知道,要让任 ...