POJ 1258:Agri-Net(最小生成树&&prim)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 38918 | Accepted: 15751 |
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
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
简单的最小生成树问题。
。题意要你求得最短的距离。
。1A水过
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath> using namespace std;
const int INF = 0x3f3f3f3f;
int map[105][105];//地图
int lowlen[105];//最短距离
int vist[105];//prim中用于看节点是否进树
int ans;
int n; void init()
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d", &map[i][j]);
memset(vist, 0, sizeof(vist));
memset(lowlen, 0, sizeof(lowlen));
} void prim()//prim算法
{
int temp;
ans = 0;
for(int i=1; i<=n; i++)
lowlen[i] = map[1][i];
vist[1] = -1;
for(int i=2; i<=n; i++)
{
temp = INF;
int k =0;
for(int j=1; j<=n; j++)
{
if( vist[j]!=-1 && temp>lowlen[j] )
{
temp = lowlen[j];
k = j;
}
}
vist[k] = -1;
ans += temp;
for(int j=1; j<=n; j++)
{
if(vist[j]!=-1 && lowlen[j]>map[k][j])
{
lowlen[j] = map[k][j];
}
}
}
printf("%d\n", ans);
} int main()
{
while(scanf("%d", &n)==1)
{
init();
prim();
} 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 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 // 顶点的最大个数 ( ...
- poj 1258 Agri-Net 解题报告
题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...
随机推荐
- 帝国CMS网站迁移方法
19大学网 我是用帝国CMS 6.0一键安装版的,在本地设计好网页后才上传到空间.期间查看了很多资料,通过两天的摸索终于上传成功,现在我把我的制作过程如实的写下来,希望对遇到同样问题的朋友能有所 ...
- HTML5 Video P2P技术研究(转)
说明:之前在Flash时代,可以基于其实现P2P的技术,也就是现在主流的视频网站用的视频技术,不过要实现P2P技术,在Flash时代有点难,且要服务器支持等等:但是现在基于HTML5技术的P2P技术使 ...
- 如何获取php错误
今天把项目放在测试服务器,但是出现一个问题,用的TP5框架,我把入口文件放在了根目录,访问的时候报错了,框架引导文件引入不了,也不报错,就是说访问不了. 所以就用了一段代码把错误获取出来了,代码如下: ...
- Delphi DevExpress下载
http://download.csdn.net/detail/wozengcong/8395285#comment
- datetimepicker只显示日期,不显示时分秒
HTML代码<div class="input-group date form_datetime form-date" data-link-field="dtp_i ...
- 两篇整合Activiti Modeler到业务系统
1. 无法进入editor. http://localhost:8080/YouPRJ/modeler/service/editor?id=2050,前提是这个id必需存在与act_re_model表 ...
- minishift的本地代码构建
看文档说支持,但实际尝试了下,失败 发现仍然是找github中的代码 找了半天,发现是在目录下有.git的隐含目录,然后copy到其他目录下删除,然后再试,发现仍然失败. 日志看是指定的目录并没有传入 ...
- 本地环境 XAMPP+phpStorm+XDebug+chrome配置和断点调试
不明白phpStorm+XAMPP+chrome组合的phpStorm配置XDebug的断点调试,很多种网页办法都看过了,可用,但没达预期.QQ群问,一个大牛很奇怪我都配置了怎么还不正确,很干脆地说远 ...
- Android面试题(2)
1.activity的生命周期 方法 描述 可被杀死 下一个 onCreate() 在activity第一次被创建的时候调用.这里是你做所有初始化设置的地方──创建视图.设置布局.绑定数据至列表等.如 ...
- jsp页面数据回显(select下拉选择框)
1.静态变量方式: <!-- 实现select标签回显 --> 1.<select name="curStatus" value="${curStatu ...