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 // 顶点的最大个数 ( ...
随机推荐
- MyBatis中关于SQL标签的用法(重用SQL 代码段)
一. 没用sql标签前的SQL映射代码: <select id="findById" resultType="cn.tedu.mybatis.entity.User ...
- redis 篇 - 键 and string
redis 进入控制台 redis-cil 需要输入密码的时候可以是用 -a redis-cil -a abcd1234 redis 数据类型 string hash list set zset( 有 ...
- luogu P3795 钟氏映射(递推)
题意 n<=107 20MB 题解 也就是给n个点,把他们一个分为一组,或两个分为一组,有多少种方法. 空间大点随便做. 我们靠递推. 一个新点,要不自己一组,要不和前面的一个点构成一组. 所以 ...
- [HNOI2004]打砖块(敲砖块)
题目:codevs1257.洛谷P1437 题目大意:有一些砖块呈倒三角形状,每块砖敲掉后有一个分数.除第一行外,敲掉一块砖必须先把上面两块砖敲掉.现在你能敲m块砖,求能得到的最大分数. 解题思路:此 ...
- Fiddler 接口测试(Composer)的使用方法
原文:Fiddler 接口测试(Composer)的使用方法 下载地址:https://www.telerik.com/download/fiddler 一.Composer简介 右侧Composer ...
- jquery validate验证remote时的多状态问题
因为远程验证用户名时可能会出现几种错误情况: 1.用户名字符非法: 2.长度超限: 3.用户名已经存在: 但remote返回的内容只能是布尔型的,即使用dataFilter来过滤也不知道如何对应的把错 ...
- 【linux驱动分析】misc设备驱动
misc设备驱动.又称混杂设备驱动. misc设备驱动共享一个设备驱动号MISC_MAJOR.它在include\linux\major.h中定义: #define MISC_MAJO ...
- LightOJ - 1132 Summing up Powers 矩阵高速幂
题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 处理开发者需求
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第1章节--SharePoint 2013 介绍 处理开发者需求 SharePoint本质上是一个平台.你 ...
- php中file_get_contents如何读取大容量文件
php中file_get_contents如何读取大容量文件 一.总结 一句话总结:使用file_get_contents()进行分段读取,file_get_contents()函数可以分段读取 1. ...