zoukankan      html  css  js  c++  java
  • poj1751

    最小生成树

    View Code
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    using namespace std;

    #define maxn 800
    #define maxm 1000

    struct Point
    {
    int x, y;
    }point[maxn];

    struct Edge
    {
    int a, b;
    }edge[maxn * maxn];

    int n, m;
    int father[maxn];

    int dist(Point a, Point b)
    {
    Point p;
    p.x = a.x - b.x;
    p.y = a.y - b.y;
    return p.x * p.x + p.y * p.y;
    }

    bool operator < (const Edge &a, const Edge &b)
    {
    return dist(point[a.a], point[a.b]) < dist(point[b.a], point[b.b]);
    }

    int getanc(int a)
    {
    if (a == father[a])
    return a;
    return father[a] = getanc(father[a]);
    }

    void merge(int a, int b)
    {
    father[getanc(a)] = getanc(b);
    }

    void input()
    {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    scanf("%d%d", &point[i].x, &point[i].y);
    for (int i = 0; i < n; i++)
    father[i] = i;
    scanf("%d", &m);
    for (int i = 0; i < m; i++)
    {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    merge(a, b);
    }
    m = 0;
    for (int i = 0; i < n - 1; i++)
    for (int j = i + 1; j < n; j++)
    {
    edge[m].a = i;
    edge[m].b = j;
    m++;
    }
    }

    void work()
    {
    for (int i = 0; i < m; i++)
    if (getanc(edge[i].a) != getanc(edge[i].b))
    {
    int a = edge[i].a;
    int b = edge[i].b;
    merge(a, b);
    printf("%d %d\n", a + 1, b + 1);
    }
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    input();
    sort(edge, edge + m);
    work();
    return 0;
    }

  • 相关阅读:
    Python从文件中读取数据
    Python中类的继承
    Python中的类(2)
    Python中的类
    自动登陆抽屉(1)
    爬取汽车之家新闻
    Django简介
    web应用,http协议简介,web框架
    CentOS7安装python3.6
    MySQL之索引
  • 原文地址:https://www.cnblogs.com/rainydays/p/2196717.html
Copyright © 2011-2022 走看看