• [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
  • 相关阅读:
    django框架——十二
    django框架——十一
    请简述一下你所了解的数据源控件有哪些
    在ASP.NET中,<%= %>和<%# %>有什么区别
    请解释ASP.NET中的web页面与其隐藏类之间的关系
    什么是viewstate,能否禁用?是否所用控件都可以禁用
    WEB控件及HTML服务端控件能否调用客户端方法?如果能,请解释如何调用
    静态类和静态方法的好处
    请写出在ASP.NET中常用的几种页面间传值的方法,并说出它们的特点。
    连接数据库主要有哪几个对象
  • 原文地址:https://www.cnblogs.com/taojy/p/7159007.html
Copyright © 2020-2023  润新知