zoukankan      html  css  js  c++  java
  • [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
  • 相关阅读:
    Hibernate + mysql 查询伪劣时:= 出现 Space is not allowed after parameter prefix ':' MySQL异常
    Linux下使用Markdown
    第十六章:Java内存模型——Java并发编程实战
    第十五章:原子变量与非阻塞机制——Java并发编程实战
    第十四章:构建自定义的同步工具——Java并发编程实战
    第十三章:显示锁——Java并发编程实战
    访问者模式——HeadFirst设计模式学习笔记
    原型模式——HeadFirst设计模式学习笔记
    第十二章:并发程序的测试——Java并发编程实战
    备忘录模式——HeadFirst设计模式学习笔记
  • 原文地址:https://www.cnblogs.com/taojy/p/7159007.html
Copyright © 2011-2022 走看看