ZOJ 1203 Swordfish MST
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的更多相关文章
- ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法
主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...
- ZOJ 1203 Swordfish(Prim算法求解MST)
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- ZOJ 1203 Swordfish
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- zoj 1203 Swordfish prim算法
#include "stdio.h". #include <iostream> #include<math.h> using namespace std; ...
- POJ 1751 Highways (ZOJ 2048 ) MST
http://poj.org/problem?id=1751 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2048 题目大 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- ZOJ 1914 Arctic Network (POJ 2349) MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1914 http://poj.org/problem?id=2349 题目大 ...
- 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 ...
随机推荐
- 洛谷——P1843 奶牛晒衣服
https://www.luogu.org/problem/show?pid=1843#sub 题目背景 熊大妈决定给每个牛宝宝都穿上可爱的婴儿装 . 于是 , 为牛宝宝洗晒衣 服就成了很不爽的事情. ...
- opera mini 改服
opera mini 改服 下载 opera 和 opera mini ftp://ftp.opera.com/pub/opera/android/mini/ ftp://ftp.opera.com/ ...
- php实现合并多个数组
php实现合并多个数组 一.总结 1.就是想c++和java里面合并数组那么简单,就是把多个数组的值赋值个一个啊,很简单 二.代码 合并多个数组,不用array_merge(),题目来于论坛. 思路: ...
- Flume的data flow(数据流)
data flow描述了数据从产生,传输.处理并最终写入目标的一条路径. 数据的采集的流向!如下图所示.
- Java 批量修改文件后缀
import java.io.*; public class test { public void reName(String path, String from, String to) { File ...
- 交叉编译工具链bash: gcc:no such file or directory
在进行交叉编译工具链安装时,有三种方法: 1.源码编译,手动安装 2.二进制可执行文件直接安装 3.直接解压工具链,手动修改环境变量 为了方便,我们多用方法3进行安装.但是问题来了,你的工具链制作时有 ...
- angular 子组件与父组件通讯
1. 子组件app-sidebar.compnent.html (click)="goProject()"设置点击事件 <mat-list-item [routerLink] ...
- ios系统和某些移动端background-attachment:fixed不兼容性
固定背景不动:background-attachment:fixed; ios系统和某些移动端background-attachment:fixed不兼容性,没有任何效果,但可以hack一下就可以了, ...
- [Firebase] Firebase Cloud Functions
Firebase cloud functions is similar to AWS lambda or serverless. You can deploy you code which wrote ...
- 三期_day03_环境搭建和客户页面_I
以下交代一下使用的框架 前端: EasyUI+Jquery+Ajax 后台: Spring+Structs2+mybatis 数据库: Oracle 使用工具: MyEclipse12+Maven 操 ...