题目链接:

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. flume遇到的问题

    Caused by: java.lang.IllegalStateException: Unable to add FlumeEventPointer [fileID=, offset=]. Queu ...

  2. B - Avoiding a disaster

    Description Percy likes to be punctual. So much so that he always keeps three watches with him, so t ...

  3. netcore的NLog使用小记

    1. 启动应用程序日志配置 修改Program.cs,在WebHostBuilder构建时配置日志 public static IWebHostBuilder CreateWebHostBuilder ...

  4. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  5. Token的生成和检验

    package TestToken; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt. ...

  6. IO模型 IO多路复用

    阻塞IO 用socket 一定会用到accept recv recvfrom这些方法正常情况下 accept recv recvfrom都是阻塞的 非阻塞IO 如果setblocking(False) ...

  7. 我的AI之路 —— 从裸机搭建GPU版本的深度学习环境

    之前一直在CPU上跑深度学习,由于做的是NLP方向所以也能勉强忍受.最近在做图像的时候,实在是扛不住了...还好领导们的支持买个虚拟机先体验下.由于刚买的机器,环境都得自己摸索,瞎搞过很多次,也走过很 ...

  8. 转---单页面应用下的JS内存管理

    正文从这开始- 内存问题对于后端童鞋而言可能是家常便饭,特别是C++童鞋.我在实习时做过半年的c++游戏客户端开发(也是前端开发哦),也见识了各式各样的内存问题,就说说我的第一个坑,当时做个需求,就是 ...

  9. EF6使用Mysql,踏过的那些坑

    在vs2013中使用mysql连接entityFramework经常会遇到这个问题:您的项目引用了最新实体框架:但是,找不到数据连接所需的与版本兼容的实体框架数据提供程序.请退出此向导,安装兼容提供程 ...

  10. SubLime Text 3 配置SublimeREPL来交互式调试程序

    1. 安装 SublimeREPL 插件 等待一下,输入sublimerepl,选择sublimeREPL,然后它就会在后台安装. 安装完之后,查看如下图 选择你要执行的*.py文件,通过这个路径,选 ...