Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45050   Accepted: 18479

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

题意简单来说就是给了一个图的邻接矩阵,求这个图的最小生成树的代价。

Prim算法模板题。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int num;
int map[102][102];
int stack[102];
int minidis[102]; int prim()
{
int i,j,s,result; memset(stack,0,sizeof(stack));
for(i=1;i<=num;i++)
{
minidis[i]=100005;
} stack[1]=1;
minidis[1]=0;
s=1;
result=0; for(i=1;i<=num-1;i++)
{
int min_all=100005;
int min_temp=0;
for(j=2;j<=num;j++)
{
if(stack[j]==0&&minidis[j]>map[s][j])
{
minidis[j]=map[s][j];
}
if(stack[j]==0&&minidis[j]<min_all)
{
min_temp=j;
min_all=minidis[j];
}
}
s=min_temp;
stack[s]=1;
result += min_all;
}
return result;
} int main()
{
int i,j; while(cin>>num)
{
for(i=1;i<=num;i++)
{
for(j=1;j<=num;j++)
{
scanf("%d",&map[i][j]);
}
}
cout<<prim()<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1258:Agri-Net Prim最小生成树模板题的更多相关文章

  1. POJ 1258 Agri-Net 【Prime】模板题

    题目链接>>> 题目大意:     给你N*N矩阵,表示N个村庄之间的距离.FJ要把N个村庄全都连接起来,求连接的最短距离(即求最小生成树).解析如下: #include <c ...

  2. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  3. POJ 1789 Truck History【最小生成树模板题Kruscal】

    题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...

  4. 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 ...

  5. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  6. POJ 1860 Currency Exchange【bellman-Ford模板题】

    传送门:http://poj.org/problem?id=1860 题意:给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换回到s,能否使本金增多. 思路:bellman-Ford模板题 ...

  7. O - 听说下面都是裸题 (最小生成树模板题)

    Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...

  8. POJ 3624 Charm Bracelet(01背包模板题)

    题目链接 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52318   Accepted: 21912 Descriptio ...

  9. POJ 1273:Drainage Ditches 网络流模板题

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63339   Accepted: 2443 ...

随机推荐

  1. restfulframework详解

    restfulframework详解 第一篇 RESTful规范

  2. HihoCoder第十周:后序遍历

    也就在大二学数据结构的时候知道了树的前序遍历.后序遍历.中序遍历.之后就忘了,在之后就是大四研究生老师考我,我当时还不知道,真够丢人的.自此之后,知道了如何通过其中两个得到第三个,但是也没有编程实现过 ...

  3. Local-Pref(本地优先属性)路由本地优先术

    Local-Pref(本地优先属性)路由本地优先术: ①:抓取感兴趣流量——前缀与访问——prefix and access ②:创建路由地图——router-map ③:第一法则——permit 1 ...

  4. PHP开发-模板的使用

    通过今天晚上半个多小时的赵老师对模板的介绍,大致对模板的使用.结构以及开发模板的思想有了个大致的了解. 为什么要开发模板:模板就是将PHP发过过程中使用到.需要操作的事物封装成对象.以便在使用到的时候 ...

  5. 四十七、在SAP中,把功能区块整合成一个函数,通过调用函数的办法使代码简洁明了

    一.我们查看上一次的代码,非常之凌乱,大体可以分为以下这几个区块 二.我们把最后的2个部分,用函数的方式来写,写法如下: 三.执行程序,和之前一样 四.输出结果

  6. 四十三、在SAP中初始化勾选值

    一.上代码 二.运行时,勾选框会被自动勾选中 三.表单如下

  7. 打包APK出现org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:lintVitalRelease'.

    AndroidS Studio打包APK时出现问题:org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':a ...

  8. softmax、交叉熵

    Softmax是用于分类过程,用来实现多分类的 它把一些输出的神经元映射到(0-1)之间的实数,并且归一化保证和为1,从而使得多分类的概率之和也刚好为1. Softmax可以分为soft和max,ma ...

  9. 2019-9-16 java上课知识整理总结(动手动脑,课后实验)

    java上课知识整理总结(动手动脑,课后实验) 一,课堂测试 1,题目:课堂测试:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件” 要求:(1)题目避免重复: (2)可定制( ...

  10. Kaggle: House Prices: Advanced Regression Techniques

    Kaggle: House Prices: Advanced Regression Techniques notebook来自https://www.kaggle.com/neviadomski/ho ...