hdu1162 Eddy's picture(prim)
2012-05-05 09:34
260 查看
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3651 Accepted Submission(s): 1793
[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
分析:经典prim,把每个坐标看成一个顶点,依次编号1至n,于是很方便的可以使用邻接矩阵了。
心得:我老是在循环(n-1)次找顶点时,总是喜欢循环 n 次,下次不要了!!!
View Code
#include<iostream> #include<cmath> #include<cstdio> #define N 110 using namespace std; struct coord{double x,y;}point ; double map ,lowcost ; bool s ; double prim(int n) { int i,j,k; double min,ans=0; s[1]=true; for(i=2;i<=n;i++) { lowcost[i]=map[1][i]; s[i]=false; } for(i=1;i<n;i++)//只要需要n-1条边,所以只能循环n-1次 { min=2000000000; for(j=2;j<=n;j++) { if(min>lowcost[j]&&!s[j]) { min=lowcost[j]; k=j; } } ans+=min;s[k]=true; for(j=2;j<=n;j++) { if(lowcost[j]>map[k][j]&&!s[j]) lowcost[j]=map[k][j]; } } return ans; } int main() { int n,i,j; double t1,t2; while(cin>>n) { for(i=1;i<=n;i++) { cin>>point[i].x>>point[i].y; } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { t1=fabs(point[i].x-point[j].x); t2=fabs(point[i].y-point[j].y); map[j][i]=map[i][j]=sqrt(t1*t1+t2*t2); } } printf("%.2lf\n",prim(n)); } return 0; }
相关文章推荐
- hdu 1162 裸最下生成树 prim
- hdu 1162 Eddy's picture(最小生成树,prim)
- HDU 1162 Prim
- HDU 1162 Prim
- hdu 1162 Prim
- HDU 1162 Eddy's picture(最小生成树-Prim)
- HDU1162 Eddy's picture【Prim】
- hdu 1162 Eddy's picture(Prim)
- hdu 1162 prim模板
- HDU1162(prim)
- hdu1162 Eddy's picture 最小生成树 prim
- hdu 1162 Eddy's picture(最小生成树prim)
- hdu1162----------- prim模板
- 【Prim 求次小生成树】HDU 4081 Qin Shi Huang's National Road System
- HDU-1233 还是畅通工程(prim)
- hdu 1863 prim初步
- HDU 1862 畅通工程(最小生成树prim)
- HDU-1879 继续畅通工程(最小生成树[Prim])
- hdu 3371 最小生成树 prim
- hdu1233 还是畅通工程 prim