题目链接:

http://poj.org/problem?id=1258

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
题意描述:
最小生成树问题。
解题思路:
使用普瑞姆算法。
AC代码:
 #include<stdio.h>
#include<string.h>
int e[][],dis[],bk[];
int main()
{
int i,j,u,v,inf,min,n,sum,count;
inf=;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&e[i][j]);
for(i=;i<=n;i++)
dis[i]=e[][i];
memset(bk,,sizeof(bk));
bk[]=;
count=;
sum=;
while(count < n)
{
min=inf;
for(j=;j<=n;j++)
{
if(bk[j]==&&dis[j]<min)
{
min=dis[j];
u=j;
}
}
bk[u]=;
count++;
sum += dis[u];
for(v=;v<=n;v++)
{
if(!bk[v]&&dis[v]>e[u][v])
{
dis[v]=e[u][v];
}
}
}
printf("%d\n",sum);
}
return ;
}

POJ 1258 Agri-Net(Prim算法求解MST)的更多相关文章

  1. ZOJ 1203 Swordfish(Prim算法求解MST)

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

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

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

  3. HDU 1863 畅通工程(Prim算法求解MST)

    题目: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现 ...

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

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

  5. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

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

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

  7. POJ 1251 Jungle Roads(Kruskal算法求解MST)

    题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...

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

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

  9. POJ 1258 Agri-Net(Prim)

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

随机推荐

  1. 中美会计准则差异比较(PRC GAAP VS US GAAP)

    http://bbs.chinaacc.com/forum-2-43/topic-2023118.html 一.中美会计准则的实质性差别    rule based vs principle base ...

  2. FastReport的使用方法

    以下是我在网上收集的 这是FastReport的主控件.它包含了调入.保存.预览和打印报表的方法.每个TfrReport控件只能包含一个单独报表. TfrReport属性描 述 DataSet-联接到 ...

  3. 在线团队协作工具+在线UML工具

    话不多说直接上https://worktile.com去看,顺便附上小众软件上面的介绍 默默增加worktile的外国原版https://trello.com/,worktile照着trello做的, ...

  4. 获取用户真实ip

    public static string GetRealIP() { string result = System.Web.HttpContext.Current.Request.Headers[&q ...

  5. EF使用MySql DBFirst产品的问题总结

    一.实体数据模型向导->新建连接->更改数据源  找不到MySql Batabase选项. 解决:需求安装以下两个插件(mysql官网都可以找到)(注意版本,后面会讲到) 1.MySql ...

  6. TDateTimePicker中Date与Time的误导

    Delphi DateTime,Date,Time TDateTimePicker DateTimeDateTimeDateTimePicker 一.DateTime,Date,Time存储方式本质上 ...

  7. webpack安装整理

    早上有点时间大概安装一下webpack,操作一下顺便把步骤记一下,乱乱的,还是记录一下吧! webpack安装步骤:1. 2. 3.一直回车,出现如下图: 4.创建src和dist文件 5.需要在np ...

  8. 复习 C++ 中类的函数指针

    函数指针这种东西,平时工作中基本上不会用到. 那函数指针会用在哪里? 下面是一些基本的用法,根据消息号调到对应的函数: #include <iostream> #include <m ...

  9. CSS3标签显示模式

    HTML标签一般分为块标签和行内标签两种类型,它们也称块元素和行内元素.具体如下: 块级元素(block-level) 每个块元素通常都会独自占据一整行或多整行,可以对其设置宽度.高度.对齐等属性,常 ...

  10. linux只读文件系统

    一般方法如下 首先试下重新挂载行不行 mount -o remount,rw /dev/sda3 不行的话用fsck,具体方法如下 1. mount命令查看变成只读文件的位置,比如/dev/sda32 ...