哈理工OJ 1630 网线 【建图+Prime算法】
来源:互联网 发布:淘宝开学季文案 编辑:程序博客网 时间:2024/06/06 09:46
题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1630
网线
Time Limit: 5000 MS Memory Limit: 32768 K
Total Submit: 82(22 users) Total Accepted: 23(20 users) Rating: Special Judge: No
Description
试设计一个网络连接某个区域中的一些地点。给定这些地点所在的位置,网线的长度就是两个地点的线段距离,假定,给定的网线可以直接或间接的连接该地区中的所有点,试为这个地区设计一个网络系统,使得该地区所有地点都可以直接或间接的连接,并且使用网线长度最短。
Input
输入一个数字n(n<1000),给出这n个点的位置(x,y),即坐标(0≤x<1000,0≤y<1000)。
Output
输出网线的最短距离,保留小数点后四位。
Sample Input
1
1 1
2
0 0
1 1
3
0 0
0 1
1 1
Sample Output
0.0000
1.4142
2.0000
【分析】用Kruskal算法会超时,单用Prime算法可以直接过。
下面是 AC代码:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;const double INF=1000000;const int MAXN=1100;bool vis[MAXN];double lowc[MAXN];double a[MAXN][MAXN];double Prime(double cost[][MAXN],int n){ double ans=0; memset(vis,false,sizeof(vis)); vis[0]=true; for(int i=1;i<n;i++) { lowc[i]=cost[0][i]; } for(int i=1;i<n;i++) { double minc=INF; int p=-1; for(int j=0;j<n;j++) { if(!vis[j]&&minc>lowc[j]) { minc=lowc[j]; p=j; } } if(minc==INF) return -1; ans+=minc; vis[p]=true; for(int j=0;j<n;j++) { if(!vis[j]&&lowc[j]>cost[p][j]) { lowc[j]=cost[p][j]; } } } return ans;}struct Point{ double x,y;}p[1005];double dist(Point p1,Point p2){ return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}int main(){ int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } memset(a,INF,sizeof(a)); for(int i=0;i<n;i++)//建图 { for(int j=i+1;j<n;j++) { a[i][j]=dist(p[i],p[j]); a[j][i]=a[i][j]; } } double re=0; re=Prime(a,n); printf("%.4lf\n",re); } return 0;}
0 0
- 哈理工OJ 1630 网线 【建图+Prime算法】
- hrbust 哈理工oj 网线【MST+Prim】
- 哈理工oj 1348 最短路径 (floyd算法)
- 哈理工OJ 1192 追逐(贪心算法)
- 哈理工OJ 1674 充电(贪心算法)
- Hust oj 1630 网线(MST)
- 哈理工OJ 2179(深搜)
- 哈理工 oj 1161 Leyni
- 哈理工OJ 1692【水题】
- 哈理工oj 2317 Game
- 哈理工oj 2002 幂集
- 哈理工oj Touring (最短路 dij算法 邻接表 + 队列 )
- 哈理工 oj 2122 旅行(map + 最短路dij算法)
- 哈理工OJ 1315 火影忍者之~大战之后(贪心算法)
- 哈理工OJ 1491 游河(网络流水题【Edmond-Karp算法】)
- 哈理工 oj 2189 节点的连接
- HLG 哈理工OJ 2074逃生
- 哈理工OJ 1926 函数式计算
- 最新Spket在线安装地址
- QT下载地址
- Ubuntu 15.04 静态IP网络配置
- uva 1363 Joseph's Problem 等差数列
- Android ExifInterface对JPG文件的exif信息进行读写
- 哈理工OJ 1630 网线 【建图+Prime算法】
- leetcode 72. Edit Distance
- Android之dagger2实现依赖注入的原理
- 性能优化——内存优化建议(RAM)
- POJ 1390 Blocks DP *
- 1-gcc 选项
- Hadoop实战演练:搜索数据分析----TopK计算(2)
- 排序算法总结
- 对象的容纳