• poj 1258 Agri-Net


    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 44147   Accepted: 18054

    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

    prime做法:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <queue>
     5 #include <stack>
     6 #include <iostream>
     7 using namespace std;
     8 #define lengthmax  100005
     9 int map[105][105];
    10 int dis[105];
    11 int main(){
    12     //freopen("D:\INPUT.txt","r",stdin);
    13     int n;
    14     while(scanf("%d",&n)!=EOF){
    15         int i,j;
    16         for(i=0;i<n;i++){
    17             dis[i]=lengthmax;
    18             for(j=0;j<n;j++){
    19                 scanf("%d",&map[i][j]);
    20             }
    21         }
    22         int m=1,mink,s=0,min,sum=0;
    23         dis[0]=0;//已经在集合里面
    24         while(m<n){//循环n-1次
    25             min=lengthmax;
    26             for(i=1;i<n;i++){
    27                 if(dis[i]>map[s][i]){//更新
    28                     dis[i]=map[s][i];
    29                 }
    30                 if(dis[i]&&min>dis[i]){
    31                     min=dis[i];
    32                     mink=i;
    33                 }
    34             }
    35             //cout<<mink<<endl;
    36             //cout<<min<<endl;
    37             sum+=min;
    38             dis[mink]=0;
    39             s=mink;
    40             m++;
    41         }
    42         cout<<sum<<endl;
    43     }
    44     return 0;
    45 }

    kruskal做法:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <queue>
     5 #include <stack>
     6 #include <algorithm>
     7 #include <iostream>
     8 using namespace std;
     9 struct node{
    10     int u,v,l;
    11 };
    12 node p[5000];
    13 int fa[105];
    14 bool cmp(node a,node b){
    15     return a.l<b.l;
    16 }
    17 int findfa(int a){
    18     if(a!=fa[a]){
    19         fa[a]=findfa(fa[a]);//attention!
    20     }
    21     return fa[a];
    22 }
    23 int main(){
    24     //freopen("D:\INPUT.txt","r",stdin);
    25     int n;
    26     while(scanf("%d",&n)!=EOF){
    27         int i,j,k;
    28         for(i=0;i<n;i++){
    29             fa[i]=i;
    30         }
    31         int num;
    32         k=0;
    33         for(i=0;i<n;i++){
    34             for(j=0;j<n;j++){
    35                 scanf("%d",&num);
    36                 if(i<j){
    37                     p[k].u=i;
    38                     p[k].v=j;
    39                     p[k++].l=num;
    40                 }
    41             }
    42         }
    43         int sum=0;
    44         num=1;
    45         sort(p,p+k,cmp);
    46 
    47         for(i=0;i<k;i++){
    48             if(num==n){
    49                 break;
    50             }
    51             int u=findfa(p[i].u);
    52             int v=findfa(p[i].v);
    53             if(u==v){
    54                 continue;
    55             }
    56             if(u>v){
    57                 fa[v]=u;
    58             }
    59             else{
    60                 fa[u]=v;
    61             }
    62             sum+=p[i].l;
    63             num++;
    64         }
    65         cout<<sum<<endl;
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    [php learn] php 从头開始学习1
    Qt实现Windows远程控制
    [core java学习笔记][第十一章异常断言日志调试]
    网络安全-安全散列函数,信息摘要SHA-1,MD5原理
    Cardboard虚拟现实开发初步(二)
    C#中Stack&lt;T&gt;类的使用及部分成员函数的源代码分析
    编程算法
    读取spring配置文件的方法(spring读取资源文件)
    Spring中的事务管理详解
    配置spring事务管理的几种方式(声明式事务)
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4645114.html
Copyright © 2020-2023  润新知