poj 1751 Highways(最小生成树)

还是简单的最小生成树。

说下题目的大意吧,给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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值