POJ 1258 Agri-Net (最小生成树+Prim)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39820 | Accepted: 16192 |
Description
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
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
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
Source
代码:(新手 勿喷)
Slyar:简介一下题意。农民要建立互联网络,目的使村庄里全部的农民连上网,而且总费用最小。多组数据。每组数据给出一个n,然后给出n * n大小的无向图的邻接矩阵表示,值表示边权。
要求输出最小生成树的权值和。
能够用Kruskal算法解决该题。用并查集检查待增加生成树的两边是否会构成回路,高速排序按权值排列边。
这里有一个优化:由于是无向图,所以矩阵是对称的,因此我们仅仅保存上三角矩阵就可以。这样到最后k的值就是边数。由循环n*n次缩减到循环k次...
只是我用的是Prim......
全是模板啦。我也不怎么会解释,尽量啦。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
#define M 101
#define INF 1000001
int map[M][M];
int n;
int Prim()
{
int s=1;
int m=1;
bool cmp[M]={0}; //标记这个点是否在子图内。
cmp[s]=1; //从第一个点開始。 int min_w; //用来找当前的最小权边。 int prim_w=0; //用来存线路的总长。
int point; //这是用来存那个即将入图的那个点的。
int low_dis[M]; //这个用来存子图到这个点的最短距离。(关键)
for(int i=1;i<=n;i++)
low_dis[i]=INF; //初始化一定要大。
while(1)
{
if(m==n) break; //假设每一个点都入图,已经将全部点连好了。 min_w=INF;
for(int i=2;i<=n;i++)
{
if(!cmp[i] && low_dis[i]>map[s][i])
low_dis[i]=map[s][i]; //假设子图有更短的才替换(关键)
if(!cmp[i] && min_w>low_dis[i])
{
min_w=low_dis[i];
point=i; //这就是找那个最小权边。并标记它的下标。
}
}
s=point;
cmp[s]=1; //这个点入图,加上权边值。
prim_w+=min_w; m++; //子图中的点数+1.
}
return prim_w;
}
int main()
{
int i,j;
while(cin>>n)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>map[i][j];
cout<<Prim()<<endl;
}
return 0;
}
我也没办法。
。。模板就这样。。
。
POJ 1258 Agri-Net (最小生成树+Prim)的更多相关文章
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- POJ 1258 Agri-Net(最小生成树,模板题)
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...
- POJ 1258 Agri-Net(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...
- POJ 1258 Agri-Net (最小生成树)
Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...
- POJ 1751 Highways(最小生成树&Prim)题解
思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- poj 1258 Agri-Net【最小生成树(prime算法)】
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44827 Accepted: 18351 Descri ...
- POJ 2485 Highways【最小生成树最大权——简单模板】
链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- 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 ...
- 最小生成树 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 // 顶点的最大个数 ( ...
随机推荐
- 简单暴力的TP5多主题方案
一个小项目,需要配置多套前端主题.解决的思路是根据域名加载不同的主题配置. 一.在应用目录 application 下创建 common 目录. 二.application/common 目录下创建 ...
- 【codeforces 314C】Sereja and Subsequences
[题目链接]:http://codeforces.com/problemset/problem/314/C [题意] 让你从n个元素的数组中选出所有的不同的非递减子数列; 然后计算比这个子数列小的和它 ...
- FZU 1980 AbOr's story
AbOr's story Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original I ...
- Lock-less buffer management scheme for telecommunication network applications
A buffer management mechanism in a multi-core processor for use on a modem in a telecommunications n ...
- 成都磨子桥技工学校 / 2016届练习区 0003:jubeeeeeat
0003:jubeeeeeat 总时间限制: 1000ms 内存限制: 256000kB 描述 众所周知,LZF很喜欢打一个叫Jubeat的游戏.这是个音乐游戏,游戏界面是4×4的方阵,会根据音乐 ...
- YII进行数据查询及类库追踪
一般处理过程: 模型进行数据操作,继承自CActiveRecord (活跃记录) AR数据库向上的封装.AR通过OOP面向对象方式操作数据库.AR须要终于转变为详细的sql语句.通过一个中间类(cri ...
- QtWebkit里RenderLayer树的绘制具体流程分析
更新:RenderLayer树的绘制对RenderObject的绘制.同一时候补足绘制阶段的描写叙述. QtWebkit里,QWebView,QWebPage和QWebFr ...
- iOS CST NSDate
好像是从ios4.1開始[NSDate date];获取的是GMT时间,这个时间和北京时间相差8个小时.下面代码能够解决问题 - (void)tDate { NSDate *date = [NSDat ...
- 智课雅思词汇---十、pend是什么意思
智课雅思词汇---十.pend是什么意思 一.总结 一句话总结:[词根含义]:悬挂,垂;称量;支付 词根:-pend-, -pens- [词根含义]:悬挂,垂;称量;支付 [词根来源]:来源于拉丁语动 ...
- 44.AngularJS Bootstrap
转自:https://www.cnblogs.com/best/tag/Angular/ Bootstrap 你可以在你的 AngularJS 应用中加入 Twitter Bootstrap,你可以在 ...