Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 44487   Accepted: 18173

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

题意:
有n个农场。已知这n个农场都互相相通。有一定的距离,如今每一个农场须要装光纤,问怎么安装光纤能将全部农场都连通起来,而且要使光纤距离最小,输出安装光纤的总距离。


思路:
最小生成树,给出的二维矩阵代表他们的距离,prim算法求解就可以。
代码:
/*
prim
Memory 204K
Time 16MS
*/
#include <iostream>
#include<cstdio>
using namespace std;
#define MAXV 101
#define inf 1<<29 int map[MAXV][MAXV],n; void prim()
{
int i,j,dis[MAXV],vis[MAXV],mi,v;
for(i=1;i<=n;i++)
{
dis[i]=map[1][i];
vis[i]=0;
}
for(i=1;i<=n;i++)
{
mi=inf;
for(j=1;j<=n;j++)
if(!vis[j] && dis[j]<mi)
{
mi=dis[j];
v=j;
} vis[v]=1;
for(j=1;j<=n;j++)
if(!vis[j] && dis[j]>map[v][j])
dis[j]=map[v][j];
}
int sum=0;
for(i=1;i<=n;i++)
sum+=dis[i]; printf("%d\n",sum);
} int main()
{
int i,j;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
prim();
}
return 0;
}







poj 1258 Agri-Net(Prim)(基础)的更多相关文章

  1. POJ 1258 Agri-Net(Prim)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  2. poj 1258 Agri-Net 最小生成树 prim算法+heap不完全优化 难度:0

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

  3. POJ 1258 Agri-Net(Prim算法)

    题意:n个农场,求把所有农场连接起来所需要最短的距离. 思路:prim算法 课本代码: //prim算法 #include<iostream> #include<stdio.h> ...

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

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

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

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

  6. POJ 1258 Agri-Net (prim水题)

    Agri-Net Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Subm ...

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

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

  9. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

随机推荐

  1. 1-jdk的安装与配置

    1- Jvm.jdk.jre之间的关系 JVM:Java虚拟机,保证java程序跨平台.(Java Virtual Machine) JRE: Java运行环境,包含JVM和核心类库.如果只是想运行j ...

  2. js hover 下拉框

    <div class="box"> <div class="a f">111111</div> <div class= ...

  3. sublime中使用markdown并实时编辑

    1.需求 想在sublime中编辑.md文件 2.步骤 找到菜单栏: 快捷键,shift + command + p,选择 Package Control:Install Package, 没有找到P ...

  4. js 简单制作键盘模拟

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head& ...

  5. C#对象初始化的探讨

    最近在弄MQ的性能监测数据埋点,无疑中用到一个Nstatsd的客户端,看到里面写过里面一种嵌套类的写法.代码如下: 客户端Client是一个密封的类,并且构造函数私有访问.然后又用一个嵌套类Curre ...

  6. 将树莓派3B+变成WiFi热点

    我有一个树莓派3B+,安装的是官方的Raspbian Stretch操作系统,该系统和Debian 9很类似,很多命令都是通用的. 接下来我将演示如何利用树莓派搭建WiFi热点,此热点就和家里的无线路 ...

  7. Fiddler抓包工具手机添加代理后连不上网

    fiddler工具给手机设置代理之后,手机竟然上不了网了,尝试了N种解决方法,终于给解决了,因此,记录下来以备不时之需. 1.fiddler给手机添加代理 打开fiddler工具->Tools- ...

  8. [Go]程序实体

    Go语言中的程序实体包括变量.常量.函数.结构体.接口 1.常见声明变量的方式 package main import ( "flag" "fmt" ) fun ...

  9. centos 7 下vnc弹出窗口太小解决方法

    使用以下参数启动 : vncserver :2 -geometry 800x600

  10. hdu 3732

    #include<stdio.h> #include<string.h> int n,m,dp[10001]; int max(int a,int b) {  return a ...