zoukankan      html  css  js  c++  java
  • POJ-1258 Agri-Net(kruskal最小生成树)

    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 60255   Accepted: 24985

    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

    Source

     
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <queue>
     6 #include <stack>
     7 #include <vector>
     8 #include <iostream>
     9 #include "algorithm"
    10 using namespace std;
    11 typedef long long LL;
    12 const int MAX=105;
    13 int n,m;
    14 int a[MAX][MAX],fa[MAX*MAX];
    15 struct Edge{
    16     int u,v;
    17     int val;
    18     bool operator <(const Edge &xx) const {
    19         return val<xx.val;
    20     }
    21 }edge[MAX*MAX];
    22 int getfather(int x){
    23     if (fa[x]==x) return x;
    24     return fa[x]=getfather(fa[x]);
    25 }
    26 void init(){
    27     int i,j;
    28     for (i=1;i<=n;i++){
    29         for (j=1;j<=n;j++){
    30             scanf("%d",&a[i][j]);
    31         }
    32     }
    33     m=0;
    34     for (i=1;i<=n;i++){
    35         for (j=i+1;j<=n;j++){
    36             edge[++m].u=i;
    37             edge[m].v=j;
    38             edge[m].val=a[i][j];
    39         }
    40     }
    41     for (i=1;i<=n;i++)
    42         fa[i]=i;
    43     sort(edge+1,edge+m+1);
    44 }
    45 int main(){
    46     freopen ("net.in","r",stdin);
    47     freopen ("net.out","w",stdout);
    48     int i,j,ans;
    49     while (~scanf("%d",&n)){
    50         init();ans=0;
    51         int tx,ty;
    52         for (i=1;i<=m;i++){
    53             tx=getfather(edge[i].u);
    54             ty=getfather(edge[i].v);
    55             if (tx!=ty){
    56                 ans+=edge[i].val;
    57                 fa[tx]=ty;
    58             }
    59         }
    60         printf("%d
    ",ans);
    61     }
    62     return 0;
    63 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    PowerDesigner将PDM导出生成WORD文档
    wrong number of arguments,java方法反射时数组参数的坑
    页面关键词Meta Keywords和描述Description的优化作用
    Java RMI之HelloWorld篇
    WebService另一种轻量级实现—Hessian 学习笔记
    RMI、Hessian、Burlap、Httpinvoker、WebService的比较
    利用Hessian如何实现Webservice
    什么是反射?反射有什么用处
    java string,需要进行首字母大写改写
    解决版本冲突-使用SVN主干与分支功能
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7279413.html
Copyright © 2011-2022 走看看