题目网址:http://poj.org/problem?id=1258

题目:

Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 60004   Accepted: 24855

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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf=;
int n,res;
int dist[],f[][];
int vis[];
void prim(){
int v=;
memset(vis, , sizeof(vis));
for (int i=; i<=n; i++) dist[i]=inf;
dist[]=;
for (int i=; i<=n; i++) {
int Min=inf;
for (int j=; j<=n; j++) {
if(dist[j]<Min && !vis[j]){
Min=dist[j];
v=j;
}
}
vis[v]=;
res+=dist[v];
for (int j=; j<=n; j++) {
dist[j]=min(dist[j],f[v][j]);//算法核心,是求遍历点到部分最小生成树的最小距离,而不是单个点
}
}
}
int main(){
while (scanf("%d",&n)!=EOF && n) {
res=;
for (int i=; i<=n; i++) {
for (int j=; j<=n; j++) {
scanf("%d",&f[i][j]);
}
}
prim();
printf("%d\n",res);
}
return ;
}

最新文章

  1. curl 模拟登录微信公众平台带验证码
  2. Linux下编译安装Apache 2.4
  3. spring ioc DI 理解
  4. Javascript 常用系统内置函数
  5. 使用DriverManager获取数据库连接
  6. 转】Maven学习总结(七)——eclipse中使用Maven创建Web项目
  7. UVA 796 - Critical Links (求桥)
  8. 获取select值
  9. MongoDB基础之五:游标
  10. C++构造函数(一)
  11. C# 从网站下载图片
  12. 对于vue和react“页面间”传递数据的理解误区
  13. Kibana-4.6.6 marvel插件license过期重新注册
  14. 版本管理工具Git(3)VS2013下如何使用git
  15. MySQL设置只读模式
  16. 【HNOI2016】序列
  17. ubuntu sudo apt-get upgrade 和 sudo apt-get dist-upgrade区别
  18. 浅谈MyBatis缓存
  19. service依赖dao的接口进行数据传输
  20. Autocad 2010+ObjectArx 2010 +Vs2010 的.net 开发设置(转)

热门文章

  1. go语言新建多维map集合
  2. jdk1.8源码阅读
  3. jvm对象内存分配
  4. github认证登陆
  5. 验证fstab文件修改是否正确
  6. 前端面试题——html与css基础篇
  7. Python网络爬虫实战(五)批量下载B站收藏夹视频
  8. Educational Codeforces Round 72 (Rated for Div. 2)
  9. Apache Thrift 的基本使用
  10. ajax 请求前后处理