hdu 1162 Eddy's picture(最小產生樹)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   for   

Problem DescriptionEddy 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? 
InputThe 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. 
OutputYour program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.  
Sample Input31.0 1.02.0 2.02.0 4.0 Sample Output3.41

題意:給你N個點的座標,求連通圖的最小距離。

思路:計算每兩個點之間的距離,然後轉化成為暢通工程題。

 1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<math.h> 6 #include<algorithm> 7 #define LL long long 8 #define mod 1e9 + 7 9 const int M = 105;10 11 using namespace std;12 13 struct node{14     int x;15     int y;16     double value;17 }a[M * M >> 1];18 19 int cun[M];20 21 double dis(double x1, double y1, double x2, double y2)22 {23     return sqrt(1.0 * (x1 - x2) * (x1 - x2) + 1.0 * (y1 - y2) * (y1 - y2));24 }25 26 bool cmp(node a, node b)27 {28     return a.value < b.value;29 }30 31 int find(int x)32 {33     return cun[x] == x ? x : cun[x] = find(cun[x]);34 }35 36 void shu(int x, int y)37 {38     int p = find(x);39     int q = find(y);40     if(p != q)41         cun[p] = q;42 }43 44 int main()45 {46     int n;47     double s[M], e[M];48     while( cin >> n )49     {50         int temp = 1;51         for(int i = 1; i <= n; ++i)52             cin >> s[i] >> e[i];53         for(int i = 1; i <= n; ++i)54             for(int j = i + 1; j <= n; ++j)55             {56                 a[temp].x = i;57                 a[temp].y = j;58                 a[temp].value = dis(s[i],e[i],s[j],e[j]);59                 temp++;60             }61         for(int i = 0; i <= n; ++i)62             cun[i] = i;63         sort(a,a + temp,cmp);64         double ans = 0;65         for(int i = 1; i < temp; ++i)66         {67             if(find(a[i].x) != find(a[i].y))68             {69                 shu(a[i].x,a[i].y);70                 ans += a[i].value;71             }72         }73         printf("%.2lf\n",ans);74     }75     return 0;76 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.