Agri-Net

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem 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
 #include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
int e[][];
int d[];
bool v[];
int main()
{
int n, i, j;
while (cin >> n)
{
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
cin >> e[i][j];
}
}
for (i = ; i <= n; i++)
{
d[i] = e[][i];
}
memset(v, , sizeof(v));
v[] = ;
int s = ;
for (i = ; i <= n - ; i++)
{
int k = -;
int mi = ;
for (j = ; j <= n; j++)
{
if (!v[j] && d[j] <= mi)
{
mi = d[j];
k = j;
}
}
if (k == -) break;
v[k] = ;
s = s + d[k];
for (j = ; j <= n; j++)
{
if (!v[j] && d[j] > e[k][j])//这一步和dijkstra不太同,不是d[j]>d[k]+e[k][j]
{
d[j] = e[k][j];
}
}
}
cout << s << endl;
}
return ;
}

POJ 1258 Agri-Net (prim水题)的更多相关文章

  1. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

  2. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  3. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

  4. poj 1004:Financial Management(水题,求平均数)

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126087   Accepted: ...

  5. POJ 3176 Cow Bowling (水题DP)

    题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...

  6. vijos 1069 新年趣事之红包 Prim水题

    描述 xiaomengxian一进门,发现外公.外婆.叔叔.阿姨……都坐在客厅里等着他呢.经过仔细观察,xiaomengxian发现他们所有人正好组成了一个凸多边形.最重要的是,他们每个人手里都拿着一 ...

  7. poj 1658 Eva's Problem(水题)

    一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...

  8. POJ 1258:Agri-Net Prim最小生成树模板题

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

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

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

随机推荐

  1. java分布式系统开关功能设计(服务升降级)

     ​问题一:在单个java系统中如何实现开关功能? ​    ​其实对于开关来说,对应Java中的类型,很好映射,就是一个boolean值,在需要做开关操作的地方,调用这个属性,判断状态,然后走相应的 ...

  2. hexo + Github 搭建问题综述

    1.Mac下安装hexo Error: Cannot find module './build/Release/DTraceProviderBindings 解决: solution 2.node s ...

  3. LA 4636 积木艺术

    https://vjudge.net/problem/UVALive-4636 题意: 给出正视图和侧视图,判断最少要用多少个立方体. 思路: 首先正视图里出现的积木个数都是必须的,记录下每一列积木的 ...

  4. UVa 10655 n次方之和(矩阵快速幂)

    https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...

  5. 51nod 1040 最大公约数的和 欧拉函数

    1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 给出一个n,求1-n这n个数,同n的最大公约数 ...

  6. /etc/apt/sources.list.d/ros-latest.list' permission denied

    换为英文的' sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu trusty main" > /etc/apt ...

  7. 一次完整的HTTP事务是怎样一个过程

    当我们在浏览器的地址栏输入 www.linux178.com,然后回车,回车这一瞬间到看到页面到底发生了什么呢? 以下过程仅是个人理解: 域名解析 --> 发起TCP的3次握手 --> 建 ...

  8. lapply

    正如前面展示的,lapply( )函数接收一个向量和一个函数作为输入参数.它将这个函数应用到向量中的每个元素,再将结果以列表的形式返回.当每次迭代都是相互独立时,这个函数就非常好用.因为在这种情况下, ...

  9. Java HashMap的工作原理

    面试的时候经常会遇见诸如:”java中的HashMap是怎么工作的”.”HashMap的get和put内部的工作原理”这样的问题. 本文将用一个简单的例子来解释下HashMap内部的工作原理. 首先我 ...

  10. 转载:【Oracle 集群】RAC知识图文详细教程(八)--Oracle 11G RAC数据库安装

    文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...