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

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+5;
typedef long long ll;
using namespace std;
struct node
{
ll x,y,cost;
}p[10005]; int pre[maxn];
int find(int x)
{
if(x==pre[x])
{
return x;
}
else
{
return pre[x]=find(pre[x]);
}
}
bool Merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return true;
}
else
{
return false;
}
} bool cmp(node x,node y)
{
return x.cost<y.cost;
}
int main()
{
int n;
while(cin>>n)
{ int x;
int cnt=0;
for(int t=1;t<=n;t++)
{
pre[t]=t;
}
for(int t=1;t<=n;t++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&x);
p[cnt].x=t;
p[cnt].y=j;
p[cnt].cost=x;
cnt++;
}
}
sort(p,p+cnt,cmp);
int c=0;
ll sum=0;
for(int t=0;t<cnt;t++)
{
if(c==n-1)
{
break;
}
if(Merge(p[t].x,p[t].y))
{
sum+=p[t].cost;
c++;
}
}
cout<<sum<<endl;
} return 0;
}

POJ-1258 Agri-Net(最小生成树)的更多相关文章

  1. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  2. POJ 1258 Agri-Net (最小生成树)

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

  3. POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)

    题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elec ...

  4. POJ 1258 Agri-Net(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...

  5. poj 1258 Agri-Net【最小生成树(prime算法)】

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44827   Accepted: 18351 Descri ...

  6. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  8. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  9. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  10. poj - 1258 Agri-Net (最小生成树)

    http://poj.org/problem?id=1258 FJ为了竞选市长,承诺为这个地区的所有农场联网,为了减少花费,希望所需光纤越少越好,给定每两个农场的花费,求出最小花费. 最小生成树. # ...

随机推荐

  1. Java Thread系列(十)Future 模式

    Java Thread系列(十)Future 模式 Future 模式适合在处理很耗时的业务逻辑时进行使用,可以有效的减少系统的响应时间,提高系统的吞吐量. 一.Future 模式核心思想 如下的请求 ...

  2. POJ 1739 Tony's Tour (DP)

    题意:从左下角到右下角有多少种走法. 析:特殊处理左下角和右下角即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000 ...

  3. 引入jQuery

    用于测试和开发(未压缩,是可读的代码)  uncompressed, 用于实际的网站中,已被精简和压缩.  minified   jQuery 1.xjQuery Migrate  过渡版jQuery ...

  4. Spring源码解析 - BeanFactory

    BeanFactory是Spring实现依赖注入的核心接口.提供应用的统一配置注册功能,实现业务开发解偶.使用getBean可以代替单例,原型设计模式. 顶重要的BeanFactory里注释写得太好了 ...

  5. Linux 基础教程 28-nc命令

    nc     nc命名netcat,直译为网络猫.在CentOS 7查看帮助的解释如下所示: ncat - Concatenate and redirect sockets 翻译过来就是可以连接和重定 ...

  6. MFC中的一般经验之谈4

    MFC中的窗口控件,都是从CWnd类中继承的.MFC以及ResourceEditor支持的所有控件称为标准控件. 在对话框资源文件夹下,然后邮件新建添加新对话框,新建对话框后要在资源视图中的对话框文件 ...

  7. 洛谷P4312 [COCI 2009] OTOCI / 极地旅行社(link-cut-tree)

    题目描述 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务. 当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mi ...

  8. tomcat mac

    在mac上安装tomcat,教程很不错:http://blog.csdn.net/j2ee_me/article/details/7928493 注意 1.要下载二进制文件,core, 2.解压后移动 ...

  9. Android-HttpsURLConnectionHelp工具类

    Java版(HttpURLConnection)Https请求工具类 public class HttpsURLConnectionHelp { /** * 加密协议 */ public static ...

  10. Centos 下安装tomcat多实例

    基础环境及JDK就不多说了,下面的目录结构以如下为准: 根目录-apps根目录-apps--tomcat根目录-apps--ins1根目录-apps--ins2 =================== ...