zoukankan      html  css  js  c++  java
  • 最小生成树模板——给出两点和权值

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<map>
     5 #include<cmath>
     6 using namespace std;
     7 typedef long long ll;
     8 int fa[10010];
     9 int n, m;
    10 struct edge {
    11     int from;
    12     int to;
    13     ll cost;
    14 }e[1000010];
    15 struct node {
    16     int x;
    17     int y;
    18 }island[10010];
    19 void init()
    20 {
    21     for (int i = 1; i <= n; i++)
    22     {
    23         fa[i] = i;
    24     }
    25 }
    26 int find(int x)
    27 {
    28     return x==fa[x] ? fa[x] : fa[x] = find(fa[x]);
    29 }
    30 void baba(int x, int y)
    31 {
    32     int fx = find(x);
    33     int fy = find(y);
    34     fa[fx] = fy;
    35 }
    36 bool same(int x, int y)
    37 {
    38     return find(x) == find(y);
    39 }
    40 
    41 bool cmp(edge a, edge b)
    42 {
    43     return a.cost < b.cost;
    44 }
    45 void Kruskal()
    46 {
    47     sort(e + 1, e + m , cmp);
    48     for (int i = 1; i < m; i++)
    49     {
    50         if (same(e[i].from, e[i].to))
    51         {
    52             continue;
    53         }
    54         baba(e[i].from, e[i].to);
    55         printf("%d %d
    ", e[i].from, e[i].to);
    56     }
    57 }
    58 
    59 int main()
    60 {
    61     scanf("%d", &n);
    62     for (int i = 1; i <= n; i++)
    63     {
    64         scanf("%d%d", &island[i].x, &island[i].y);
    65     }
    66     init();
    67     int M;
    68     scanf("%d", &M);
    69     for (int i = 1; i <= M; i++)
    70     {
    71         int a, b;
    72         scanf("%d%d", &a, &b);
    73         baba(a, b);
    74     }
    75     m = 1;
    76     for (int i = 1; i <= n; i++)
    77     {
    78         for (int j = i + 1; j <= n; j++)
    79         {
    80             if (j == i)
    81             {
    82                 continue;
    83             }
    84             ll dis = (island[i].x - island[j].x)*(island[i].x - island[j].x) + (island[i].y - island[j].y)*(island[i].y - island[j].y);
    85             e[m].cost = dis;
    86             e[m].from = i;
    87             e[m].to = j;
    88             m++;
    89         }
    90     }
    91     Kruskal();
    92     return  0;
    93 }

    POJ 1751

  • 相关阅读:
    pagefile.sys
    Oracle数据库同义词
    oracle临时表
    修改Oracle并行度
    Oracle 反键索引/反向索引
    Ajax基础2
    DOM高级
    面向对象--高级
    面向对象---中级
    面向对象-初级
  • 原文地址:https://www.cnblogs.com/fengzhongzhuifeng/p/10726144.html
Copyright © 2011-2022 走看看