还是简单的最小生成树。
说下题目的大意吧,给n个点的坐标(x,y),然后是给出那几个点已经是连通的,这块很简单,提前用并查集处理下即可,。让你输出MST的边的信息,每个信息是两个端点。
sad....一开始把数据范围750看成了75.。。。迷之自信,贡献了一次RE...
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const double eps = 1e-8;
const int N = 800;
int root[N];
int dcmp(double x)
{
if(fabs(x) < 0)
{
return 0;
}
else
{
return x > 0 ? 1 : -1;
}
}
struct Point
{
double x,y;
Point(const double& x = 0,const double& y = 0):x(x),y(y){}
};
struct Edge
{
int from,to;
double dis;
Edge(const int& from, const int& to,const double& dis):from(from),to(to),dis(dis){}
bool operator < (const Edge& res) const
{
return dis < res.dis;
}
};
typedef Point Vector;
Vector operator - (Vector A, Vector B)
{
return Vector(A.x - B.x , A.y - B.y);
}
Vector operator + (Vector A, Vector B)
{
return Vector(A.x + B.x , A.y + B.y );
}
Vector operator * (Vector A , double p)
{
return Vector (A.x * p , A.y * p);
}
Vector operator / (Vector A , double p)
{
return Vector (A.x / p , A.y / p);
}
double Dot(Vector A, Vector B)
{
return A.x * B.x + A.y* B.y;
}
double Cross(Vector A , Vector B)
{
return A.x * B.y - A.y *B.x;
}
double Length(Vector A)
{
return Dot(A,A);
}
void InitSet(int n )
{
for(int i = 1 ; i <= n ; i++)
{
root[i] = i ;
}
}
int FindSet(int x)
{
if(x != root[x])
{
root[x] = FindSet(root[x]);
}
return root[x];
}
void UnionSet(int a,int b)
{
int x = FindSet(a);
int y = FindSet(b);
if(x != y)
{
root[x] = y;
}
}
vector<Edge>edge;
void MST(int n)
{
for(int i = 0 ; i < edge.size() ; i++)
{
int from = edge[i].from;
int to = edge[i].to;
if(FindSet(from) != FindSet(to))
{
cout<<from<<" "<<to<<endl;
UnionSet(from,to);
}
}
cout<<endl;
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int n;
vector<Point>point;
double x,y;
scanf("%d",&n);
for(int i = 0; i < n ; i++)
{
scanf("%lf %lf",&x,&y);
point.push_back(Point(x,y));
}
for(int i = 0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
edge.push_back(Edge(i+1,j+1,Length(point[i] - point[j])));
}
}
int Q;
InitSet(n);
scanf("%d",&Q);
int from,to;
for(int i = 0 ; i < Q ; i++)
{
scanf("%d %d",&from,&to);
UnionSet(from,to);
}
sort(edge.begin(),edge.end());
MST(n);
return 0;
}