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;
    }

  • 相关阅读:
    js 提升
    omnipay支付--支付宝支付
    laravel 配置sql日志
    laravel monlog配置
    php openssl相关加密解密 验签代码
    laravel validator提示信息中文化
    opendevops_codo项目研究
    Python学习笔记
    shell编程总结
    关于JeeSite开源软件
  • 原文地址:https://www.cnblogs.com/rainydays/p/2196717.html
Copyright © 2011-2022 走看看