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> ...
随机推荐
- Linux 内核中的 GCC 特性
https://www.ibm.com/developerworks/cn/linux/l-gcc-hacks/ GCC 和 Linux 是出色的组合.尽管它们是独立的软件,但是 Linux 完全依靠 ...
- Visual Studio自动生成XML类和JSON类
Visual Studio 2013及以上版本提供了一个非常实用的功能.可以根据xml文档或json文档自动生成类.有了这个工具反序列化时就不用再手动写类并且实现了强类型操作. 步骤 1. 准备一份j ...
- 如何释放 DB_RECOVERY_FILE_DEST_SIZE
转自原文 如何釋放 DB_RECOVERY_FILE_DEST_SIZE,有删减 oracle默認安裝之後,如何沒有手動設置歸檔路徑(alter system set log_archive_dest ...
- 修改activityMQ的登录账与密码
登录下管理员页面,ip根据实际的来 URL : http://127.0.0.1:8161/admin/ 默认账户密码都是admin 账户密码修改在conf文件夹下的jetty-realm.prope ...
- Jquery JS 正确的比较两个数字大小的方法
if(2 > 10){ alert("不正确!");} 此比较不会是想要的结果:它相当于2 >1,把10的第一位取出来比较. 解决方 法: if(eval(2) &g ...
- JS中eval函数的使用
/*************************************************注册用户证件号 复选框 combox循环赋值**************************** ...
- iOS:CALayer核心动画层
CALayer:核心动画层 简介: Core Animation 是跨平台的,支持iOS环境和Mac OS X环境 学习核心动画之前,需要先理解CALayer,因为核心动画操作的对象不是UIView, ...
- 使用免安装版本在windows上手动安装PostgreSQL
PostgreSQL支持管理员直接手动安装数据库,给用户提供了更大的方便. 1. 在PostgreSQL官方网站上下载免安装二进制的包,名字类似于postgresql-*.*.*.*-bina ...
- 算法之排序Low B三人组
有序区:有的地方的数据已经完全变得有顺序,我们把这部分区域的数据成为有序区无序区:有的地方的数据依旧无序,我们把这部分数据成为无序区时间复杂度:用来估计算法运行时间的一个式子(单位)空间复杂度:用来评 ...
- 3D游戏图形技术解析(7)——视差映射贴图(Parallax Mapping)【转】
http://www.cnblogs.com/taotaobujue/articles/2781371.html 视差映射贴图(Parallax Mapping) ● 传统纹理贴图的弊端 纹理贴图大家 ...