ZOJ:1203 Swordfish
来源:互联网 发布:系统工程师 软件开发 编辑:程序博客网 时间:2024/05/16 10:19
题意:给你n个城市的坐标,要求你输出连通所有城市的最小线路长度。
思路:最小生成树。注意每两个样例之间有换行。
#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>#define MAXN 105using namespace std;struct Edge{ int a,b; double weight;};bool cmp(Edge x,Edge y){ return x.weight<y.weight;}int father[MAXN];int find(int p){ return p==father[p]?p:(father[p]=find(father[p]));}int main(){ int n,kase=0; while(scanf("%d",&n)&&n) { double x[MAXN],y[MAXN]; for(int i=0; i<n; ++i) scanf("%lf%lf",&x[i],&y[i]); Edge p[MAXN*MAXN]; int nn=0; for(int i=0; i<n; ++i) for(int j=i+1; j<n; ++j) { p[nn].a=i; p[nn].b=j; p[nn++].weight=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } sort(p,p+nn,cmp); for(int i=0; i<=n; ++i) father[i]=i; double ans=0; for(int i=0; i<nn; ++i) { int ta=p[i].a,tb=p[i].b; if(find(ta)!=find(tb)) { ans+=p[i].weight; father[find(ta)]=find(father[tb]); } } if(kase) printf("\n"); printf("Case #%d:\n",++kase); printf("The minimal distance is: %.2lf\n",ans); } return 0;}
- ZOJ 1203 Swordfish ( prim )
- zoj 1203 Swordfish(Prim!)
- zoj 1203 Swordfish【prim】
- Prim zoj 1203 Swordfish
- zoj - 1203 - Swordfish
- zoj 1203 Swordfish
- zoj 1203 Swordfish MST
- ZOJ 1203 - Swordfish
- ZOJ-1203 Swordfish
- ZOJ-1203-Swordfish
- ZOJ:1203 Swordfish
- zoj 1203 Swordfish
- ZOJ 1203 Swordfish MST
- ZOJ 1203 Swordfish
- zoj 1203 Swordfish
- ZOJ-1203 Swordfish
- zoj 1203 Swordfish
- zoj 1203 Swordfish
- ios发推广码
- sql*plus 常用命令
- 考研日记2013.10.05
- 一个简单的登陆界面程序
- 关于野指针(迷途指针),空指针,通用指针
- ZOJ:1203 Swordfish
- 字符串编辑距离
- 如何将int转换成String 在java中
- 内存管理内幕
- 多线程同步
- Java HashMap实现详解
- 线性时间内查找第k个最小数
- Rabbit的安装、配置、监控
- unix高级环境编程 系统数据文件与信息 --chap6