暢通工程再續 hdu1875

來源:互聯網
上載者:User

 http://acm.hdu.edu.cn/showproblem.php?pid=1875

#include<iostream>//2235167 2010-03-23 16:21:03 Accepted 1875 312MS 352K 1567 B C++ 悔惜晟
#include<algorithm> //效率真的不高,演算法還是有待給進,耗時主要在哪裡呢??
#include<cstdio>
#include<cmath>
int aa[4951];
using namespace std;

 struct stu1
 {
  int x1;
  int y1;
 }df1[103];
 
 struct stu2
 {
  int x2;
  int y2;
  double distance ;
 }df2[4951];
 
 int cmp(stu2 a, stu2 b)
 {
  return a.distance < b.distance;
 }
 
 int find(int x)
 {
  int r = x;
  while(r !=aa[r])
   r = aa[r];
  return r;
 }
 
 void mergy(int a, int b)
 {
  if(a > b)
   aa[b] = a;
  else
   aa[a] = b;
 }
 
int main()
{
 int t, n, i, j, count, ww;
 double sum, dis;
 scanf("%d", &t);
 while(t--)
 {
  scanf("%d", &n);
  for(i = 0; i < n; i++)
   scanf("%d%d", &df1[i].x1, &df1[i].y1);
   count = 0;
  for(i = 0 ; i < n-1; i++)
   for(j = i+1; j < n; j++)
   {
   
     dis = sqrt(1.0*((df1[i].x1 - df1[j].x1) * (df1[i].x1 - df1[j].x1) + (df1[i].y1 - df1[j].y1) * (df1[i].y1 - df1[j].y1)));
         if(dis < 10 || dis > 1000)
           continue;
      else
         {
         df2[count].x2 = i + 1;
      df2[count].y2 = j + 1;
      df2[count].distance = dis;
      count++;
     }
      
   }
 
   sort(df2,df2+count,cmp);
   for(i = 1 ; i <= n*(n-1)/2; i++)
    aa[i] = i;
   ww = 0;
   sum = 0;//忘記了初始話
   for(i = 0 ;i < count ; i++)
   {
       int a = find(df2[i].x2);
       int b = find(df2[i].y2);
       if(a != b)
       {
        sum += df2[i].distance;
        mergy(a, b);
        ww++;
    }
   }
   if(ww == n-1)
       printf("%0.1lf/n", 100*sum);
   else
                printf("oh!/n");
 }
  
}

 

/*
#include<iostream>//2235212 2010-03-23 16:25:50 Time Limit Exceeded 1875 1000MS 408K 1648 B C++ 悔惜晟 
#include<algorithm>
#include<cstdio>
#include<cmath>
int aa[9910];
using namespace std;

 struct stu1
 {
  int x1;
  int y1;
 }df1[103];
 
 struct stu2
 {
  int x2;
  int y2;
  double distance ;
 }df2[9910];
 
 int cmp(stu2 a, stu2 b)
 {
  return a.distance < b.distance;
 }
 
 int find(int x)
 {
  int r = x;
  while(r !=aa[r])
   r = aa[r];
  return r;
 }
 
 void mergy(int a, int b)
 {
  if(a > b)
   aa[b] = a;
  else
   aa[a] = b;
 }
 
int main()
{
 int t, n, i, j, num, count, flag;
 double sum, dis;
 scanf("%d", &t);
 while(t--)
 {
  scanf("%d", &n);
  for(i = 0; i < n; i++)
  scanf("%d%d", &df1[i].x1, &df1[i].y1);
  count = 0;
  flag = 0;
  for(i = 0 ; i < n - 1; i++)
  {
   int tt = 0;
   for(j = 0; j < n; j++)
   {
    if(i != j)//這裡處理的很不好,多做很多沒有用的判斷
    {
     dis = sqrt(1.0*((df1[i].x1 - df1[j].x1) * (df1[i].x1 - df1[j].x1) + (df1[i].y1 - df1[j].y1) * (df1[i].y1 - df1[j].y1)));
         if(dis < 10 || dis > 1000)
           t++;
      else
         {
         df2[count].x2 = i + 1;
      df2[count].y2 = j + 1;
      df2[count].distance = dis;
      count++;
     }
    }
      
   }
   if(tt == n-1)
   {
    flag = 1;
    break;
   }
  }
  if( flag == 1)
   printf("oh!/n");
  else
  {
   sort(df2,df2+count,cmp);
   for(i = 1 ; i <= n*(n-1)/2; i++)
    aa[i] = i;
   sum = 0;
   for(i = 0 ;i < count ; i++)
   {
       int a = find(df2[i].x2);
       int b = find(df2[i].y2);
       if(a != b)
       {
        sum += df2[i].distance;
        mergy(a, b);
    }
   }
   printf("%0.1lf/n", 100*sum);
  }
  
 }
}
 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.