• [poj1258]Agri-Net


    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 22125   Accepted: 8681

    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


    最小生成树裸题
    prim/kruskal都行
    这个用prim实现的

      1 #include <stdio.h>
      2 #include <string.h>
      3 #define MaxInt 0x3f3f3f3f
      4 #define N 110
      5 
      6 //创建map二维数组储存图表,low数组记录每2个点间最小权值,visited数组标记某点是否已访问
      7 
      8 int map[N][N],low[N],visited[N];
      9 
     10 int n;
     11 
     12  
     13 
     14 int prim()
     15 
     16 {
     17 
     18     int i,j,pos,min,result=0;
     19 
     20     memset(visited,0,sizeof(visited));
     21 
     22 //从某点开始,分别标记和记录该点
     23 
     24     visited[1]=1;pos=1;
     25 
     26 //第一次给low数组赋值
     27 
     28     for(i=1;i<=n;i++)
     29 
     30         if(i!=pos) low[i]=map[pos][i];
     31 
     32 //再运行n-1次
     33 
     34     for(i=1;i<n;i++)
     35 
     36     {
     37 
     38 //找出最小权值并记录位置
     39 
     40      min=MaxInt;
     41 
     42      for(j=1;j<=n;j++)
     43 
     44          if(visited[j]==0&&min>low[j])
     45 
     46          {
     47 
     48              min=low[j];pos=j;
     49 
     50          }
     51 
     52 //最小权值累加
     53 
     54     result+=min;
     55 
     56 //标记该点
     57 
     58     visited[pos]=1;
     59 
     60 //更新权值
     61 
     62     for(j=1;j<=n;j++)
     63 
     64         if(visited[j]==0&&low[j]>map[pos][j])
     65 
     66             low[j]=map[pos][j];
     67 
     68     }
     69 
     70     return result;
     71 
     72 }
     73 
     74  
     75 
     76 int main()
     77 
     78 {
     79 
     80     int i,v,j,ans;
     81 
     82     while(scanf("%d",&n)!=EOF)
     83 
     84     {
     85 
     86 //所有权值初始化为最大
     87 
     88         memset(map,MaxInt,sizeof(map));
     89 
     90         for(i=1;i<=n;i++)
     91 
     92             for(j=1;j<=n;j++)
     93 
     94             {
     95 
     96                 scanf("%d",&v);
     97 
     98                 map[i][j]=map[i][j]=v;
     99 
    100             }
    101 
    102             ans=prim();
    103 
    104             printf("%d
    ",ans);
    105 
    106     }
    107 
    108     return 0;
    109 
    110 }
    View Code
  • 相关阅读:
    计算机安装Fedora操作系统——Win10+Linux双系统
    性能测试——压力测试指标
    系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    在 数学吧 看到一个 极限题
    东方学帝 和 K歌之王 的 科学观 和 科学方法 的 对比
    走一走 欧拉先生 走过 的 路
    推导一个 经典物理 里 的 黑洞 的 坍缩半径
    四色定理 太简单了 , 来 玩 n 维空间 里 的 x 色定理
    今天看到了一个 求 平面图形 Centroid 的 办法
    记录一下这几天的一些讨论
  • 原文地址:https://www.cnblogs.com/taojy/p/7159007.html
Copyright © 2020-2023  润新知