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

Kruskal 算法,借助并查集
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 101
#define INF 0x3f3f3f3f
int pre[MAXN];
struct Edge
{
int u,v,w;
}edge[MAXN*MAXN/];
int tol;
void addedge(int u,int v,int w)
{
edge[tol].u = u;
edge[tol].v = v;
edge[tol++].w = w;
}
bool cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int find(int x)
{
if(pre[x]==-)
return x;
else
return pre[x] = find(pre[x]);
}
int Kruskal(int n)
{
memset(pre,-,sizeof(pre));
sort(edge,edge+tol,cmp);
int cnt = ;
int ans = ;
for(int i=;i<tol;i++)
{
int u = edge[i].u;
int v = edge[i].v;
int w = edge[i].w;
int t1 = find(u),t2 = find(v);
if(t1!=t2)
{
ans+=w;
pre[t1] = t2;
cnt++;
}
if(cnt==n-) break;
}
if(cnt<n-) return -;
else return ans;
}
int main()
{
int n,d;
while(cin>>n)
{
//if(n==0) break;
tol = ;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>d;
if(j>i)
addedge(i,j,d);
}
}
int ans = Kruskal(n);
cout<<ans<<endl;
}
return ;
}

最新文章

  1. windows或mac上对iOS设备截图
  2. java中的代码块执行顺序
  3. CSS实现不固定宽度和高度的自动居中
  4. mac 如何让文件隐藏
  5. 【转】Ubuntu 上编译Android出现cannot find -lstdc++解决办法
  6. vue+websocket+express+mongodb实战项目(实时聊天)
  7. 福州大学W班-助教总结
  8. Vue.js中集成summernote
  9. HTML 中使用 JavaScript
  10. AR 前言
  11. ili9325--LCD寄存器配置研究
  12. Android中使用Thread线程与AsyncTask异步任务的区别
  13. StringTokenizer 的性能看来真的不用担心
  14. Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)
  15. C#IIS网站应用程序池启动回收停止 .
  16. centos上部署nginx服务
  17. vue-router教程一(安装篇)
  18. [LeetCode 题解]: LetterCombinations
  19. dirname和shell常用命令
  20. ubuntu常用 命令

热门文章

  1. 树形DP UVA 1292 Strategic game
  2. USB接口大百科:看完你就分得清充电线了
  3. C/C++自实现的函数(memset, memcpy, atoi)
  4. c++类的内存布局
  5. [译]Cookies Without Chocolate Chips
  6. elasticsearch 查询优化
  7. Tuple类型的使用
  8. MVC的学习-EF的认识
  9. TypeError: string indices must be integers, not str
  10. canvas一周一练 -- canvas绘制中国银行标志(4)