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 // 顶点的最大个数 ( ...
随机推荐
- React-Router-API中文介绍
React-Router API 以下内容翻译自react-router/doc/API.md,方便使用时查看,之前的学习都是能够工作即可,但一些内在发生的行为并不知晓,借此理解一番: ##Compo ...
- 在javascript中对于this指向的再次理解
总所周知,function () {}函数体内的this对象指向的是调用该函数的对象,那么我们看一下这个例子 <script> var length = 3; function fn () ...
- js判断浏览器的环境(pc端,移动端,还是微信浏览器)
window.navigator.userAgent用来区分设备和浏览器 <!DOCTYPE html> <html> <head> <meta charse ...
- [洛谷P1920]成功密码
题目大意:给你n和x($n\leq 10^{18},0<x\leq 1$),要你求$\sum_{i=1}^n\frac{x^i}{i}$. 解题思路:首先n大到要用long long存,暴力肯定 ...
- Python学习————列表的增删改查
增加:li.append(对象):追加 注:print(li.append())--->是Noneli.insert(索引,对象):插入到相应位置li.extend(对象):可迭代的添加到尾部, ...
- ASP.NET-Razor语法03
ASP.NET MVC中使用Razor语法 @{} @{ string s ="super xiao lizi"; @s; // 里面的这个@代表着在页面上输出这个s // 我记 ...
- datatable 前台和后台数据格式
datatable是很强大的前台表格插件,前台定义好表格格式后,后台须要返回指定格式的json数据! 例如以下: 首先是js的定义: var oTable = $('#sample_editable_ ...
- Linux路由表的抽象扩展应用于nf_conntrack
思想 标准IP路由查找的过程为我们提供了一个极好的"匹配-动作"的例程. 即匹配到一个路由项.然后将数据包发给该路由项指示的下一跳.假设我们把上面对IP路由查找的过程向上抽象一个层 ...
- hdu-2871
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- [BZOJ1858] [SCOI2010] 序列操作 解题报告 (线段树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 Description lxhgww最近收到了一个01序列,序列里面包含了n个数, ...