POJ-1258 Agri-Net(kruskal最小產生樹)

來源:互聯網
上載者:User

標籤:logic   val   mem   amp   accept   its   sam   require   freopen   

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

40 4 9 214 0 8 179 8 0 1621 17 16 0

Sample Output

28

Source

USACO 102 
 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\n",ans);61     }62     return 0;63 }

 

POJ-1258 Agri-Net(kruskal最小產生樹)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.