Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 64912   Accepted: 26854

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

[Submit]   [Go Back]   [Status]  
[Discuss]

最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题。我们首先用通俗的语言解释它的定义:

对于有n个节点的有权无向连通图,寻找n-1条边,恰好将这n个节点相连,并且这n-1条边的权值之和最小。

对于MST问题,通常常见的解法有两种:Prim算法   或者  Kruskal算法+并查集

对于最小生成树,一定要注意其定义是在无向连通图的基础上,如果在有向图中,那么就需要另外的分析,单纯用无向图中的方法是不能得出正确解的,这一点我在比赛中确实吃过亏

好了,进入正题:

Prim算法:(基于点的贪心思路)由于是基于点的算法,因此适合于稠密图,一下给出代码没有经过堆优化,时间复杂度为O(N^2)

  记原图为G,生成树图为MST,其中G的节点个数为n个

  算法描述如下:

  1. 任取G中的一点,加入MST中——这一步的作用是选择一个节点作为整个算法的起点
  2. 采用贪心策略,将刚刚加入的节点记为u,以u为中心,检查与u相连且没有加入MST的节点(未访问过的节点),选择权值最小的边,如果有多条边的权值均最小,则任取一条边。——贪心策略,选择局部最优
  3. 将所选择的边中,不在MST中的那个节点,加入MST——举例来说,比如(u,v)是当前与u相连,v不再MST中,且权值最小的边,则边(u,v)被选中,并将v加入MST。
  4. 如果步骤2-3被执行了n-1次,则退出,反之则返回到步骤2。——由于Prim算法初始化时加入了起点,而步骤2-3每执行一次都会加入一个新的节点,所以只需判断执行次数。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std; //inf为路径权上界,maxn为图的临接矩阵的点数
//vis是记录是否访问过,cost[i]记录到达第i个节点的最小代价
const int inf=0x7fffffff,maxn=101;
int G[maxn][maxn],vis[maxn],cost[maxn],n;
//len为MST长度
int prim(){
memset(vis,0,sizeof(vis));
//加入起始节点
int pos=1,min=inf,len=0,cnt=n;
vis[1]=1;
for(int v=2;v<=n;v++)cost[v]=G[pos][v];
//加入剩余n-1个节点
while(--cnt){
for(int i=1;i<=n;i++)if(!vis[i]&&cost[i]<min){
pos=i;min=cost[i];
}
len+=min;vis[pos]=1;
//以新加入的节点为中心,更新权值信息
for(int i=1;i<=n;i++)if(!vis[i]&&G[pos][i]<cost[i])
cost[i]=G[pos][i];
min=inf;
}
return len;
} int main()
{
//freopen("in.txt","r",stdin);
//int n;
while(cin>>n&&n)
{
memset(cost,0,sizeof(cost));
memset(G,0,sizeof(G));
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>G[i][j];
}
}
cout<<prim()<<endl;
}
}

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

  1. POJ 1258:Agri-Net Prim最小生成树模板题

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

  2. poj 1258 建光迁问题 最小生成树

    题意:给全村建光纤,求花费最小 思路:最小生成树,树相对于图来说就是没有环 m用来存图 v判断是否访问 low用来存两点间的最短距离 给low赋值  for(i=1;i<=n;i++){if(i ...

  3. Codeforces 632F - Magic Matrix(暴力 bitset or Prim 求最小生成树+最小瓶颈路)

    题面传送门 开始挖老祖宗(ycx)留下来的东西.jpg 本来想水一道紫题作为 AC 的第 500 道紫题的,结果发现点开了道神题. 首先先讲一个我想出来的暴力做法.条件一和条件二直接扫一遍判断掉.先将 ...

  4. POJ 1258 Agri-Net(Prim算法)

    题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> ...

  5. POJ 1258 Agri-Net(Prim)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  6. prim求最小生成树

    一直以来只会Kruskal prim和dijkstra很像 只不过prim维护的是最短的边,而dijkstra维护的是最短的从起点到一个点的路径 同时prim要注意当前拓展的边是没有拓展过的 可以用堆 ...

  7. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  8. POJ 1258 Agri-Net (prim水题)

    Agri-Net Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Subm ...

  9. Poj 2421 Constructing Roads(Prim 最小生成树)

    题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...

随机推荐

  1. tomcat配置访问项目时不需要加项目名称

    原文:http://blog.csdn.net/coolcoffee168/article/details/52582770 java web部署后,访问项目的时候,需要在地址中添加项目名称,那么如何 ...

  2. burpsuite破解版

    来源:http://www.vuln.cn/8847

  3. 【Nginx】如何建立新连接

    处理新连接事件的回调函数是ngx_event_accept,原型如下: void ngx_event_accept(ngx_event_t *ev) 具体流程如下: 1)首先调用accept方法试图建 ...

  4. Linux——系统调用笔记1

    底层文件访问:    进程:运行中的程序,它有一些与值关联的文件描述符,有多少个文件描述符取决于系统配置情况.    当一个程序开始运行时,一般会打开三个文件描述符:        0:标准输入    ...

  5. Datagrid接收JSON数据格式

    开打View下面的Shared创建一个视图模版(母版页)<!DOCTYPE html> <html> <head> <title>Main</ti ...

  6. 大括号对struct进行初始化

    1 partial initialization 即所谓的部分初始化. 这个时候,无论该struct变量是static的还是automic的,未显式初始化的成员都会被初始化为默认值.

  7. 编译FreePascal源代码(摘录自邮件询问)

    为了尝试编译FreePascal,我搜了官方文档,并给几位作者都发了邮件询问,目前结果如下: http://wiki.lazarus.freepascal.org/Getting_Lazarus#Co ...

  8. order by 特殊排序技巧

    if object_id('tempdb..#temp') is not null drop table #temp ), col2 )) insert into #temp ' ' ' ' go - ...

  9. (14)javaWeb中的HttpServletResponse类详解

    如果希望了解请求和响应的详细内容,可以看我的“HTTP协议”系列文章 响应体的简单概述: a,响应报文结构: b,常见的状态码,返回服务器处理的结果: c,常见的响应头: HttpServletRes ...

  10. SPFA 算法详解( 强大图解,不会都难!) (转)

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便 派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径 ...