zoukankan      html  css  js  c++  java
  • POJ 2421 Constructing Roads 克鲁斯卡尔(Kruskal)算法

    F - Constructing Roads
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

    Input

    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j. 

    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

    Output

    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

    Sample Input

    3
    0 990 692
    990 0 179
    692 179 0
    1
    1 2
    

    Sample Output

    179

    解题思路: 将已经连通的道路的开销设为0就可以,这样就不会造成道路的丢失!

    解题代码:

    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 int map[maxn][maxn];
    12 
    13 struct node
    14 {
    15     int x, y;
    16     int w;
    17     bool operator < (const node T) const
    18     {
    19         return w < T.w;
    20     }
    21 }town[maxp];
    22 
    23 int Find (int x)
    24 {
    25     if (x != fa[x])
    26         return fa[x] = Find (fa[x]);
    27     return x;
    28 }
    29 
    30 void Uni(int x,  int y)
    31 {
    32     if (x == y) return;
    33     if (rank[x] > rank[y])
    34     {
    35         fa[y] = x;
    36     }
    37     else
    38     {
    39         if (rank[x] == rank[y])
    40             rank[y] ++;
    41         fa[x] = y;
    42     }
    43 }
    44 
    45 int main ()
    46 {
    47     int n, i, j, cun, x, k;
    48     int tempa, tempb;
    49     long long total;
    50     while (~scanf ("%d", &n))
    51     {
    52         cun = 0;
    53         for (i = 0; i < n; i ++)
    54         {
    55             for (j = 0; j < n; j ++)
    56             {
    57                 scanf ("%d", &map[i][j]);
    58             }
    59         }
    60     
    61         scanf ("%d", &k);
    62         while (k --)
    63         {
    64             scanf ("%d%d", &i, &j);
    65             map[i-1][j-1] = map[j-1][i-1] = 0;
    66         }
    67         for (i = 0; i < n; i ++)
    68         {
    69             for (j = i+1; j < n; j ++)
    70             {
    71                 town[cun++] = (node){i, j, map[i][j]};
    72             }
    73         }
    74         sort (town, town + cun);
    75         for (i = 0; i < cun; i ++)
    76         {
    77             rank[i] = 0;
    78             fa[i] = i;
    79         }
    80         total = 0;
    81         for (i = 0; i < cun; i ++)
    82         {
    83             tempa = Find(town[i].x);
    84             tempb = Find(town[i].y);
    85             if (tempa != tempb)
    86             {
    87                 total += town[i].w;
    88                 Uni(tempa, tempb);
    89             }
    90         }
    91         printf ("%lld\n", total);
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    注册、登录、忘记密码实战
    python3错误:format() takes at most 2 arguments
    Charles手机抓包简要步骤
    VARCHAR2(N CHAR)与VARCHAR2(N)的区别
    关于VI一些常用的操作
    LINUX下 基于 Socket 的 UDP 和 TCP 编程具体实现
    VC++6.0实现文本格式的转换保存
    crt的sftp使用用于Windows与Linux之间的通讯
    pl/sql 导出脚本与使用
    在oracle10g下启动服务报 Permission denied错误解决方法
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3034396.html
Copyright © 2011-2022 走看看