POJ 1258 Agri-Net(Prim求最小生成树)
|
Agri-Net
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 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个
算法描述如下:
- 任取G中的一点,加入MST中——这一步的作用是选择一个节点作为整个算法的起点
- 采用贪心策略,将刚刚加入的节点记为u,以u为中心,检查与u相连且没有加入MST的节点(未访问过的节点),选择权值最小的边,如果有多条边的权值均最小,则任取一条边。——贪心策略,选择局部最优
- 将所选择的边中,不在MST中的那个节点,加入MST——举例来说,比如(u,v)是当前与u相连,v不再MST中,且权值最小的边,则边(u,v)被选中,并将v加入MST。
- 如果步骤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求最小生成树)的更多相关文章
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- poj 1258 建光迁问题 最小生成树
题意:给全村建光纤,求花费最小 思路:最小生成树,树相对于图来说就是没有环 m用来存图 v判断是否访问 low用来存两点间的最短距离 给low赋值 for(i=1;i<=n;i++){if(i ...
- Codeforces 632F - Magic Matrix(暴力 bitset or Prim 求最小生成树+最小瓶颈路)
题面传送门 开始挖老祖宗(ycx)留下来的东西.jpg 本来想水一道紫题作为 AC 的第 500 道紫题的,结果发现点开了道神题. 首先先讲一个我想出来的暴力做法.条件一和条件二直接扫一遍判断掉.先将 ...
- POJ 1258 Agri-Net(Prim算法)
题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> ...
- POJ 1258 Agri-Net(Prim)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- prim求最小生成树
一直以来只会Kruskal prim和dijkstra很像 只不过prim维护的是最短的边,而dijkstra维护的是最短的从起点到一个点的路径 同时prim要注意当前拓展的边是没有拓展过的 可以用堆 ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- POJ 1258 Agri-Net (prim水题)
Agri-Net Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total Subm ...
- Poj 2421 Constructing Roads(Prim 最小生成树)
题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...
随机推荐
- Friefox清除旧的网页缓存
Ctrl + F5 适用于调试网页编码时,不断以旧设置显示页面
- [RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS
When subscribers create new "inner" sources and subscriptions, you run the risk of losing ...
- java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException 遇到这样的问题 ...
- centos+nginx+php-fpm+php include fastcgi_params php页面能訪问但空白,被fastcgi_params与fastcgi.conf害慘了
今天在centos上折腾这块是发现老是訪问页面时,浏览器中提示是200 ok.且訪问html后缀却是正常出现内容. 可是訪问php后缀却返回空白页面,同一时候查看全部的log没有发现不论什么出错信息; ...
- Javascript调试命令
Javascript调试命令——你只会Console.log() ? Console 对象提供对浏览器控制台的接入(如:Firefox 的 Web Console).不同浏览器上它的工作方式是不一样的 ...
- IDEA失效的解决办法
1.根据下图进行操作即可解决
- SSH常见错误
错误一: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml] ...
- XMU 1056 瞌睡 vs 听课 【动态规划】
1056: 瞌睡 vs 听课 Time Limit: 500 MS Memory Limit: 64 MBSubmit: 19 Solved: 6[Submit][Status][Web Boar ...
- 使用PXE安装CentOS7
1.环境 本文使用VMware 虚拟机进行实验. 点击VMware--编辑--虚拟网络编辑器,新建VMnet15,选择仅主机模式,取消勾选DHCP服务(因为这里使用自己的DHCP服务).我这里配好后是 ...
- inexact rename detection was skipped due to too many files
https://stackoverflow.com/a/28064699 error: add_cacheinfo failed to refresh for path 'LISA.Kentico.U ...