Advertisement
kcku

11228 - Transportation system

Jul 25th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Edge{ int a, b, d; } edge[500000];
  7.  
  8. bool cmp(const Edge &a, const Edge &b)
  9. {
  10.     return a.d<b.d;
  11. }
  12. int parent(int p, int *root)
  13. {
  14.     return p==root[p]?p:root[p]=parent(root[p], root);
  15. }
  16. int main()
  17. {
  18.     int cases;
  19.     scanf("%d", &cases);
  20.  
  21.     for(int n, r, cnt=1; cnt<=cases && scanf("%d%d", &n, &r); cnt++)
  22.     {
  23.         double road=0, rail=0;
  24.         int state=n, num=0, x[n], y[n], root[n];
  25.  
  26.         for(int i=0; i<n; i++)
  27.             scanf("%d%d", x+i, y+i), root[i]=i;
  28.  
  29.         for(int i=1; i<n; i++)
  30.             for(int j=0; j<i; j++)
  31.                 edge[num++]={i, j, (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])};
  32.  
  33.         sort(edge, edge+num, cmp);
  34.  
  35.         for(int i=0, t=1; i<num && t<n; i++)
  36.         {
  37.             int a=parent(edge[i].a, root), b=parent(edge[i].b, root);
  38.  
  39.             if(a!=b)
  40.             {
  41.                 root[a]=b, ++t;
  42.  
  43.                 if(edge[i].d<=r*r)
  44.                     road+=sqrt(edge[i].d), --state;
  45.                 else
  46.                     rail+=sqrt(edge[i].d);
  47.             }
  48.         }
  49.         printf("Case #%d: %d %.lf %.lf\n", cnt, state, road, rail);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement