hdu 1162 Eddy's picture
2013-04-25 10:38
141 查看
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4866 Accepted Submission(s): 2411
Problem Description
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?
Input
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.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41 题意:给出n个点,求连通这n个点所用线段的最小长度。典型的最小生成树题目(可作为模版使用),用prim算法:•假设N=(V,{E})是连通图,TE是N上最小生成树中边的集合。算法从U={u0}(u0∈V),TE={}开始,重复执行下述操作:在所有u∈U, v ∈V-U的边(u,v)∈E中找一条代价最小的边(u0,v0)并入集合TE,同时v0并入U,直至U=V为止。此时TE中必有n-1条边,则T=(V,{TE})为N的最小生成树。 AC代码:#include <iostream> #include <cstring> #include <cmath> #include <cstdio> #define N 110 #define INF 10000000 using namespace std; bool v ; double map ,lowcost ; int n; struct points { double x,y; } a ; double prim() { double sum=0,m; int x; for(int i=0; i<n; i++) lowcost[i]=map[0][i]; memset(v,false,sizeof(v)); v[0]=true; for(int i=1; i<n; i++) { m=INF; for(int j=0; j<n; j++) if(!v[j]&&lowcost[j]<m) m=lowcost[x=j]; sum+=m; v[x]=true; for(int j=0; j<n; j++) if(!v[j]&&map[x][j]<lowcost[j]) lowcost[j]=map[x][j]; } return sum; } double d(points a,points b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main() { while(cin>>n) { 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++) cin>>a[i].x>>a[i].y; for(int i=0; i<n; i++) for(int j=i+1; j<n; j++) map[i][j]=map[j][i]=d(a[i],a[j]); double ans=prim(); printf("%.2lf\n",ans); } return 0; }
相关文章推荐
- HDU 1162 Prim
- hdu 1162(Kruskal)
- hdu 1162 Eddy's picture(最小生成树算法)
- hdu 1162
- hdu 1162(最小生成树)
- HDU 1162 Eddy's picture(MST)
- 杭电Hdu 1162 Eddy's picture
- HDU1162-Eddy's picture(最小生成树)
- HDU 1162 Eddy's picture MST(基础)
- HDU 1162 Prim
- HDU 1162 Eddy's picture
- hdu 1162
- hdu 1162
- hdu 1162 Eddy's picture(最小生成树)
- HDU 1162 Eddy's picture (Kruscal最小生成树)
- hdu1162 Eddy's picture(prim)
- hdu 1162
- hdu-1162 Eddy's picture
- hdu 1162 Prim
- Hdu 1162 Eddy's picture【最小生成树】