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. BZOJ 4103 [Thusc 2015]异或运算 (可持久化01Trie+二分)

    题目大意:给你一个长方形矩阵,位置$i,j$上的数是$a_{i}\;xor\;b_{j}$,求某个子矩阵内第$K$大的值 最先想的是二分答案然后验证,然而是$O(qnlogmloga_{i})$,不出 ...

  2. 【PRML学习笔记】第四章:分类的线性模型

    一.基础概念 线性分类模型:决策面(decision boundary)是输入向量的线性函数 目标类别的表示"1 of K" :$ t = (0,1,0,0,0)^T$ 二.分类问 ...

  3. Linux 程序包管理-RPM

    程序简介:  POSIX(Portable Openratin System)跨平台系统:不同操作系统平台的标准C库(glibc)都是遵循POSIX规范的,这样基于标准库开发程序的源代码可以夸平台编译 ...

  4. linux进程管理之轻量级进程(四)

    在Linux中,轻量级进程可以是进程,也可以是线程.我们所说的线程,在Linux中,其实是轻量级进程之间共享代码段,文件描述符,信号处理,全局变量时: 如果不共享,就是我们所说的进程. 进程是资源管理 ...

  5. 【CS round 34】Minimize Max Diff

    [题目链接]:https://csacademy.com/contest/round-34/task/minimize-max-diff/ [题意] 给你n个数字; 数组按顺序不下降; 让你删掉k个数 ...

  6. java 自定义实现base64编码转换

    1.base64编码转换 所谓base64编码,即按照规则把字符转化为"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 ...

  7. JConsole远程监控Tomcat7

    下面技术应用于最优质的水果的鲜果篮 一.设置服务端: 1.增加Listener到conf/server.xml <Listener className="org.apache.cata ...

  8. css 清楚浮动的8种方式

    清除浮动是每个 web前台设计师必须掌握的机能. css清除浮动大全,共8种方法. 浮动会使当前标签产生向上浮的效果,同一时候会影响到前后标签.父级标签的位置及 width height 属性.并且相 ...

  9. Ext4.1 chart的使用

    var reportsPanel = Ext.create('Ext.panel.Panel', { id:'reportsPanel',    layout: 'fit',    tbar: [{ ...

  10. DDos游戏行业受攻击最多

    游戏行业遭遇DDOS攻击现状. 游戏一直是最易遭受黑客攻击的行业,高居全年DDOS攻击的48%.大规模攻击居多,平均值均超过100Gbps,攻击峰值急速上升,同比2015年增加了137.1%,其中攻击 ...