Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39820   Accepted: 16192

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.


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

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another.
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

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

Source

USACO 102

代码:(新手 勿喷)

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)的更多相关文章

  1. POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)

    题目链接: 传送门 Agri-Net Time Limit: 1000MS     Memory Limit: 10000K Description Farmer John has been elec ...

  2. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  3. POJ 1258 Agri-Net(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...

  4. POJ 1258 Agri-Net (最小生成树)

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

  5. POJ 1751 Highways(最小生成树&Prim)题解

    思路: 一开始用Kruskal超时了,因为这是一个稠密图,边的数量最惨可能N^2,改用Prim. Prim是这样的,先选一个点(这里选1)作为集合A的起始元素,然后其他点为集合B的元素,我们要做的就是 ...

  6. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

  7. poj 1258 Agri-Net【最小生成树(prime算法)】

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44827   Accepted: 18351 Descri ...

  8. POJ 2485 Highways【最小生成树最大权——简单模板】

    链接: http://poj.org/problem?id=2485 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  9. 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. 最小生成树 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 // 顶点的最大个数 ( ...

随机推荐

  1. 安卓、safari和微信各个浏览器的设计标准

  2. 百度蜘蛛IP地址到底代表什么含义?

    百度蜘蛛IP地址到底代表什么含义,是不是不同的ip地址所代表的含义不一样呢?对权重和抓取是否有影响?哪些是无效的蜘蛛,哪些是站长工具的蜘蛛,那些又是百度自己真正的蜘蛛?百度蜘蛛,是百度搜索引擎的一个自 ...

  3. 被我忽略许久的set

    心塞,set一直是我忽略的一个数据结构 1.生成一个set: 1) set(iterable) 传入一个可以迭代的数据结构: eg:字符串;元组;列表,字典 2) {v1,v2,.......,vn} ...

  4. (WC2018模拟十二)【FJOI2016集训Day7T3】Xor-Mul棋盘

    是不是应该第100篇博文纪念一下? 题解: 本质简单题...但是我没仔细看这题... 观察它的两个式子,都是xor完再乘以某个数,意味着d数组的每个二进制位对答案的贡献都是独立的,可以每一位分开处理. ...

  5. [agc004d]salvage robot

    题意: 别问我谁翻译的 虫合虫莫国的领土我们可以抽象为H*W的笼子,在这虫合土上,有若干个机器人和一个出口,其余都是空地,每次虫合虫莫会要求让所有的机器人向某个方向移动一步,当机器人移动到出口时会被虫 ...

  6. [SHOI2012]魔法树

    题目:洛谷P3833. 题目大意:给你一棵树,有两种操作:1.给两个点和它们之间的最短路上的所有点加上一个值:2.询问以某个点为根的子树的子树和.你需要实现这个功能. 解题思路:如果只有最后才询问的话 ...

  7. BZOJ 2246 [SDOI2011]迷宫探险 (记忆化搜索)

    题目大意:太长了,略 bzoj luogu 并没有想到三进制状压 题解: 3进制状压陷阱的状态,0表示这种陷阱的状态未知,1已知危险,2已知不危险 然后预处理出在当前状态下,每种陷阱有害的概率,设为$ ...

  8. FastDFS图片服务器搭建

    *FastDFS图片服务器搭建准备:1.需要libfastcommon安装包 选择最新稳定版(libfastcommon-1.0.36.tar.gz)2.需要FastDFS安装包 选择最新稳定版(fa ...

  9. Linux 磁盘管理及分区

    硬盘结构和基础知识  扇区(Sector)为最小的物理储存单位,每个扇区为512 bytes,将扇区组成一个圆就是磁道(track),不同磁盘的相同磁道组成磁柱(Cylinder),磁柱是分区(par ...

  10. STM32是如何进入中断服务函数xxx_IRQHandler的

    今天在看stm32的中断,一时间不理解stm32主函数是如何进入中断函数的,按C编程的理解,会有个特定的入口之类的,但是看demo过程中没有发现入口. 以串口中断服务函数void USART1_IRQ ...