畅通工程再续 hdu1875

本文详细解析了使用最小生成树算法解决特定问题的过程,并通过具体的编程实现进行了验证。重点介绍了如何利用距离比较进行节点间连接的选择,以确保最终形成的网络满足特定条件。此外,还对比了不同实现方式对算法效率的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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);
  }
  
 }
}
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值