HDU 1875 畅通工程再续

10 10
20 20

1 1
2 2
1000 1000

1414.2
oh!

### C++

/*跟畅通工程和还是畅通工程基本思路一样，不过把两点的坐标更换成两点的距离即可*/
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define maxn 0x3f3f3f3f
using namespace std;
double mat[105][105];
int m,n;
struct xd
{
int x,y;
}zxs[102];
void prim()
{
double mst[105],minn,low[105],sum=0;
int i,j,pos,ss;
for(i=2;i<=m;i++)
{
low[i]=mat[1][i];
}
for(j=2;j<=m;j++)
{   minn=maxn;
pos=0;
for(i=2;i<=m;i++)
{
if(minn>low[i]&&low[i]!=0)
{
minn=low[i];
pos=i;
}
}
if(minn==maxn)
{
cout<<"oh!\n";
return ;
}
low[pos]=0;
sum=sum+minn;
for(i=2;i<=m;i++)
{
if(mat[pos][i]<low[i])
{
low[i]=mat[pos][i];
}
}
}
printf("%.1lf\n",sum*100);
return ;
}
int main()
{
int i,j,cost,j1,i1,k,a;
double b,c,d;
cin>>a;
while(a--)
{
cin>>m;
for(i1=0;i1<=m;i1++)
for(j1=0;j1<=m;j1++)
mat[i1][j1]=maxn;
for(k=1;k<=m;k++)
{
cin>>zxs[k].x>>zxs[k].y;
for(i1=1;i1<k;i1++)
{
b=sqrt((zxs[k].x-zxs[i1].x)*(zxs[k].x-zxs[i1].x)+(zxs[k].y-zxs[i1].y)*(zxs[k].y-zxs[i1].y));
if(b<mat[k][i1]&&b>=10&&b<=1000)
{
mat[k][i1]=mat[i1][k]=b;
}
}
}
prim();
}
return 0;
}

2022-12-15 11:09

2022-12-21 22:36

2022-12-22 18:27

2022-12-16 21:55

2022-12-26 14:32

2022-12-15 17:07

2022-12-09 12:02

2022-12-10 20:52