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