zoukankan      html  css  js  c++  java
  • POJ 1258 AgriNet 克鲁斯卡尔(Kruskal)算法&并查集

    J - Agri-Net
    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

    Description

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
    The distance between any two farms will not exceed 100,000. 

    Input

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    Sample Output

    28

     解题代码:

    View Code
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <iostream>
     5 using namespace std;
     6 
     7 const int maxn = 110;
     8 const int maxp = 5000;
     9 
    10 int rank[maxp], fa[maxp];
    11 
    12 struct node
    13 {
    14     int x, y;
    15     int w;
    16     bool operator < (const node T) const
    17     {
    18         return w < T.w;
    19     }
    20 }town[maxp];
    21 
    22 int Find (int x)
    23 {
    24     if (x != fa[x])
    25         return fa[x] = Find (fa[x]);
    26     return x;
    27 }
    28 
    29 void Uni(int x,  int y)
    30 {
    31     if (x == y) return;
    32     if (rank[x] > rank[y])
    33     {
    34         fa[y] = x;
    35     }
    36     else
    37     {
    38         if (rank[x] == rank[y])
    39             rank[y] ++;
    40         fa[x] = y;
    41     }
    42 }
    43 
    44 int main ()
    45 {
    46     int n, i, j, cun, x, k;
    47     int tempa, tempb;
    48     long long total;
    49     while (~scanf ("%d", &n))
    50     {
    51         cun = 0;
    52         for (i = 0; i < n; i ++)
    53         {
    54             for (j = 0; j < n; j ++)
    55             {
    56                 scanf ("%d", &x);
    57                 if (i < j)
    58                 town[cun++] = (node){i, j, x};
    59             }
    60         }
    61         sort (town, town + cun);
    62         for (i = 0; i < cun; i ++)
    63         {
    64             rank[i] = 0;
    65             fa[i] = i;
    66         }
    67         total = 0;
    68         for (i = 0; i < cun; i ++)
    69         {
    70             tempa = Find(town[i].x);
    71             tempb = Find(town[i].y);
    72             if (tempa != tempb)
    73             {
    74                 total += town[i].w;
    75                 Uni(tempa, tempb);
    76             }
    77         }
    78         printf ("%lld\n", total);
    79     }
    80     return 0;
    81 }
     
  • 相关阅读:
    单例模式
    建造者模式
    工厂方法模式
    原型模式
    适配器模式
    桥接模式
    装饰模式
    组合模式
    多线程的学习与GDI的学习
    我们复习.Net的这些日子里
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3034403.html
Copyright © 2011-2022 走看看