Eddy's picture(prime+克鲁斯卡尔)
2015-07-09 09:38
471 查看
Eddy's picture
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 4 Accepted Submission(s) : 1
[align=left]Problem Description[/align]
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
[align=left]Input[/align]
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point. Input contains multiple test cases. Process to the end of file.
[align=left]Output[/align]
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
[align=left]Sample Input[/align]
3 1.0 1.0 2.0 2.0 2.0 4.0
[align=left]Sample Output[/align]
3.41
类似与畅通工程里面的百岛湖那个题,刚开始没理解题意,写错了,最后又wa两遍,point数组没开大;
克鲁斯卡尔代码:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; int map[110]; struct Point{ int start,end; double len; }point[10010]; int cmp(Point a,Point b){ return a.len<b.len; } struct dot{ double x,y; }dot[110]; int find(int x){ int r=x; while(r!=map[r])r=map[r]; int i=x,j; while(i!=r)j=map[i],map[i]=r,i=j; return r; } double distant(double x1,double y1,double x2,double y2){ double x=x2-x1,y=y2-y1,L; L=sqrt(x*x+y*y); return L; } int main(){ int n,c; double distance; while(~scanf("%d",&n)){ // memset(map,0,sizeof(map)); // memset(point,0,sizeof(point)); for(int i=1;i<=n;++i)map[i]=i; for(int i=1;i<=n;++i)scanf("%lf%lf",&dot[i].x,&dot[i].y);c=0; for(int i=1;i<=n;++i){ for(int j=1;j<i;++j){point[c].start=j;point[c].end=i; point[c].len=distant(dot[j].x,dot[j].y,dot[i].x,dot[i].y); c++; } } sort(point,point+c,cmp);distance=0; for(int i=0;i<c;++i){ int f1,f2; f1=find(point[i].start);f2=find(point[i].end); if(f1!=f2)map[f1]=f2,distance+=point[i].len; } printf("%.2lf\n",distance); } return 0; }
prime:
#include<stdio.h> #include<string.h> #include<math.h> const int INF=0x3f3f3f3f; const int MAXN=110; int n; double answer; double map[MAXN][MAXN],low[MAXN]; int vis[MAXN]; double gd(double ax,double ay,double bx,double by){ double x=bx-ax,y=by-ay; double s=x*x+y*y; return sqrt(s); } void prime(){ int k; double temp; memset(vis,0,sizeof(vis)); vis[0]=1; for(int i=0;i<n;i++)low[i]=map[0][i]; for(int i=0;i<n;i++){ temp=INF; for(int j=0;j<n;j++) if(!vis[j]&&temp>low[j]) temp=low[k=j];//k=写错地方了,又错了半天 if(temp==INF){ printf("%.2lf\n",answer); break; } answer+=temp; vis[k]=1; for(int j=0;j<n;j++) if(!vis[j]&&low[j]>map[k][j]) low[j]=map[k][j]; } } int main(){ double dx[MAXN],dy[MAXN]; while(~scanf("%d",&n)){ answer=0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) map[i][j]=INF; for(int i=0;i<n;i++) scanf("%lf%lf",&dx[i],&dy[i]); for(int i=0;i<n;i++) for(int j=i+1;j<n;j++){ double dis=gd(dx[i],dy[i],dx[j],dy[j]); //printf("%lf\n",dis); if(dis<map[i][j]) map[i][j]=map[j][i]=dis; } prime(); } return 0; }
相关文章推荐
- [.net 面向对象程序设计进阶] (10) 序列化(Serialization)(二) 通过序列化博客园文章学习XML的序列化
- 软件测试—小白第二天
- IE6浏览器下如何div的高度
- android菜鸟学习笔记18----Android数据存储(二)SharedPreferences
- 关于百度地图poi检索不出数据,onGetPoiResult不执行(其他功能类似)
- android几种存储方式理解心得,
- sql server 2008 评估期已过期如何升级
- 在前台给textbox设置readonly=true属性后,避免后台取不到值的3种解决方法。
- OpenGL编程指南(第七版)
- 学院培训c#第一天
- javascript+ajax实现产品页面加载信息
- win7以上系统,web项目安装未完成
- HBuilder开发App教程05-滴石和websql
- cout的输出格式初探
- linux文本界面../和./的区别
- UML时序图
- python获取局域网占带宽最大3个ip的方法
- ZOOKEEPER原理
- 玄鸟
- 初窥Linux 之 我最常用的20条命令