链接:

http://poj.org/problem?id=1258

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/I

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45751   Accepted: 18835

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>
using namespace std; const int N = ;
const int INF = 0xfffffff; int n, J[N][N], dist[N], vis[N]; int Prim()
{
int i, j, ans=;
dist[]=;
memset(vis, , sizeof(vis));
vis[]=; for(i=; i<=n; i++)
dist[i]=J[][i]; for(i=; i<n; i++)
{
int index=, MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
index=j; MIN=dist[j];
}
}
vis[index]=;
ans += MIN;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]>J[index][j])
dist[j]=J[index][j];
}
}
return ans;
} int main ()
{
while(scanf("%d", &n)!=EOF)
{
int i, j; memset(J, , sizeof(J)); for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d", &J[i][j]); int ans=Prim(); printf("%d\n", ans);
}
return ;
}

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

  1. 【裸最小生成树】 模板 poj 1258

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #def ...

  2. 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 ...

  3. 最小生成树 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 // 顶点的最大个数 ( ...

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

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

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

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

  6. poj 1258 最小生成树 模板

    POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...

  7. POJ 1258 最小生成树

    23333333333 完全是道水题.因为是偶自己读懂自己做出来的..T_T.prim的模板题水过. DESCRIPTION:John竞选的时候许诺会给村子连网.现在给你任意两个村子之间的距离.让你求 ...

  8. poj 1258 Agri-Net 最小生成树 kruskal

    点击打开链接 Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33733   Accepted: 13539 ...

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

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

随机推荐

  1. 迷你MVVM框架 avalonjs 学习教程21、双向绑定链

    avalon的双向绑定机制,是通过一条依赖链实现.此依赖链最底层是监控属性.监控数组,中层是计算属性.监控函数,再上点是求值函数,最上层是视图刷新函数. 所谓计算属性,监控属性,监控函数属性,我们改变 ...

  2. 迷你MVVM框架 avalonjs 学习教程18、一步步做一个todoMVC

    大凡出名的MVC,MVVM框架都有todo例子,我们也搞一下看看avalon是否这么便宜. 我们先从react的todo例子中扒一下HTML与CSS用用. <!doctype html> ...

  3. request传递参数

    当客户请求时,Servlet容器创建SrevletRequest对象(用于封装客户的请求信息),这个对象将被容器作为service()方法的参数之一传递给Srevlet,Servlet可以利用Serv ...

  4. JavaScipt测试调研

    JavaScript测试调研 前言 与其他语言相似,JavaScript的测试也会包括代码审查.单元测试等内容.本文就JavaScript的测试调研了一些测试工具和测试框架. 相对于其他很多高级语言语 ...

  5. 启用SQL Server 2008的专用管理员连接(DAC)

    参考:http://technet.microsoft.com/zh-cn/library/ms178068(v=SQL.105).aspx 问题: 一个在我们公司实习的DBA向我询问如何开启SQL ...

  6. electron sendInputEvent keyboard

    https://github.com/electron/electron/issues/5005 webview.getWebContents().sendInputEvent({ type: 'ch ...

  7. vcf格式简介

    1)背景 伴随着大规模的基因分型及测序工程的产生(例如1000 Genomes Project),之前的信息贮存格式例如gff文件它记录了每一个基因的详细信息,其中许多基因信息在基因组之间是共享的,而 ...

  8. HSTS详解

    1. 缘起:启用HTTPS也不够安全 有不少网站只通过HTTPS对外提供服务,但用户在访问某个网站的时候,在浏览器里却往往直接输入网站域名(例如www.example.com),而不是输入完整的URL ...

  9. HTML5 historyState pushState、replaceState

    DOM中的window对象通过window.history方法提供了对浏览器历史记录的读取,让你可以在用户的访问记录中前进和后退. 从HTML5开始,我们可以开始操作这个历史记录堆栈. 1.Histo ...

  10. models渲染字典&form表单上传文件&ajax上传文件

    {# {% for u in teacher_d.keys %}#} {# {% for u in teacher_d.values %}#} {% for k,u in teacher_d.item ...