程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 杭電--1162--Eddys picture--並查集

杭電--1162--Eddys picture--並查集

編輯:C++入門知識

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5178    Accepted Submission(s): 2566         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    #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; int father[111111]; double s; struct ssss {  double a,b; }ss[111]; struct dddd {  int a,b;  double x; }dd[5000]; int Find(int a) {  return a==father[a]?a:father[a]=Find(father[a]); } void Union(int i) {  int a=Find(dd[i].a),b=Find(dd[i].b);  if(a!=b)father[a]=b,s+=dd[i].x; //只有沒有連通的才能進行這一步,所以每次都是需要連通的最短距離 } bool cmp(const dddd &a,const dddd &b) {  return a.x<b.x; } int main (void) {  int n,i,j,k,l;  while(cin>>n)  {   for(i=0;i<n;i++)    cin>>ss[i].a>>ss[i].b;   for(i=0;i<111111;i++)father[i]=i; //初始化   for(i=l=0;i<n;i++)    for(j=i+1;j<n;j++)    {     dd[l].a=i,dd[l].b=j;     double x=ss[i].a-ss[j].a,y=ss[i].b-ss[j].b;     dd[l++].x=sqrt(x*x+y*y);  //老規矩,把距離存兩點一起    }    sort(dd,dd+l,cmp);    for(i=0,s=0;i<l;i++)     Union(i); //並起來    printf("%.2f\n",s);  }  return 0; }  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved