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 ...
随机推荐
- Linux面试题完整修订附加答案
册一: 1.Linux挂载Winodws共享文件夹 第一步:先在Windows上创建一个共享目录 Windows系统IP是172.16.18.56;共享文件夹:E:\test ...
- Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的使用方法总结
具体解读Jquery各Ajax函数: $.get(),$.post(),$.ajax(),$.getJSON() 一 $.get(url,[data],[callback]) 说明:url为请求地 ...
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
题目链接:http://www.patest.cn/contests/pat-a-practise/1001 题面: 1001. A+B Format (20) Calculate a + b and ...
- Rust 1.7.0 语法基础 sep_token 和 non_special_token
一.分隔符 sep_token 指的是分隔符, 是除了 * 和 + 之外的不论什么符号,通常情况下是使用 , 逗号. 比如: 宏的多个參数分隔,以下代码中的逗号就是 sep_token (target ...
- 算法4-10:BST平衡二叉树的删除操作
偷懒方法 public void delete(Key key) { insert(key, null); } 这样的方法就是将key相应的值覆盖成null.当读取该键值的时候将会返回null. 这是 ...
- V4L学习
http://blog.csdn.net/wangrunmin/article/details/7764768# http://blog.sina.com.cn/s/blog_a44175a90101 ...
- Buildroot构建指南——根文件系统(Rootfs)【转】
本文转载自; 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Buildroot构建指南——根文件系统(Rootfs) Buildroot的Rootfs构建流程有一个大 ...
- bzoj3272: Zgg吃东西&&3267: KC采花
口胡 我们容易得到一个费用流的做法,流出k的流量分配给各个点,各个点向下一个点流费用为它的价值的边,然后汇总到ed 观察发现对于流一次,相当于选择了一个区间 如果流了反向边,相当于减去了这一段 可以用 ...
- YTU 2559: “心脏出血”
2559: "心脏出血" 时间限制: 1 Sec 内存限制: 128 MB 提交: 5 解决: 2 题目描述 2014年4月,一个开源加密库OpenSSL的严重漏洞" ...
- 整型变量修饰符,char类型数据存储原理,字节数,
//------------------整型变量修饰符 修饰符(int short long longlong signed unsigned)所有修饰符都是用来修整形 int 4short %hd ...