1. 程式人生 > >HDU 1875: 暢通工程再續

HDU 1875: 暢通工程再續

consola fixed end php lin fin tor edge style


///@author Sycamore
///@date 9/16/2017
///@link http://acm.hdu.edu.cn/showproblem.php?pid=1875
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-7;
struct Edge
{
int from,to;
double length;
friend bool operator<(const Edge &u,const Edge &v)
{
return u.length<v.length;
}
}edge[5000
];

int c,n=0,parent[100];
void AddEdges()
{
cin>>c;
vector<pair<int,int>>island(c);
for(auto &e:island)cin>>e.first>>e.second;
for(int i=0;i<c;i++)
for(int j=i+1;j<c;j++)
{
double dist=hypot(island[i].first-island[j].first,island[i].second-island[
j].second);
if(dist>10-eps&&dist<1000+eps)
edge[n++]=Edge{i,j,dist};
}
}
int Find(int x)
{
return parent[x]==-1?x:parent[x]=Find(parent[x]);
}
void Union(int x,int y)
{
parent[Find(x)]=Find(y);
}
double Kruskal()
{
double price=0;
sort(edge,edge+n);
fill(parent,parent+c,
-1);
int counter=0;
for(int i=0;i<n;i++)
{
int u=Find(edge[i].from),v=Find(edge[i].to);
if(u==v)continue;
else
{
counter++;
Union(u,v);
price+=edge[i].length;
}
}
if(counter<c-1)return -1;
return price*100;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
n=0;
AddEdges();
double result=Kruskal();
if(result<0)
cout<<"oh!\n";
else cout<<fixed<<setprecision(1)<<result<<\n;
}
return 0;
}

HDU 1875: 暢通工程再續