POJ 1258 Agri-Net
2016-04-16 21:40
393 查看
Agri-Net
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
Sample Output
最小生成树
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 48946 | Accepted: 20307 |
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
最小生成树
#include <stdio.h> #include <string.h> int map[105][105]; int dist[105]; bool vis[105]; int N; int main() { int i, j, f, min, sum; while(scanf("%d", &N) != EOF) { for(i = 1; i <= N; i++) for(j = 1; j <= N; j++) scanf("%d", &map[i][j]); memset(vis, false, sizeof(vis)); memset(dist, 0x3f, sizeof(dist)); dist[1] = 0; sum = 0; for(i = 1; i <= N; i++) { f = -1; min = 0x3f3f3f3f; for(j = 1; j <= N; j++) if(!vis[j] && dist[j] < min) min = dist[f = j]; if(f == -1) break; vis[f] = true; sum += min; for(j = 1; j <= N; j++) if(!vis[j] && dist[j] > map[f][j]) dist[j] = map[f][j]; } printf("%d\n", sum); } return 0; }
相关文章推荐
- opencv漫水填充算法
- VS2015 xamarin android axml 文件添加智能提示
- 简单脱壳教程笔记(10) --- 手脱EXE32PACK壳
- Android中的Handler和AsyncTask的区别
- Linux下LAMP服务搭建
- java8的Effectively final
- python-MySQL学习笔记-第五章-利用Connector/Python来修改数据库
- treap树---营业额统计
- 数据结构_对称矩阵
- iosiOS中的自动布局——autoLayout
- hdu5666 BestCoder Round #80
- 运行机制归档
- hdu5666 BestCoder Round #80
- 基于Windows系统的VMWare Workstation及Linux-CentOS系统下载及安装最全ui详解关联
- eclipse在线安装svn插件
- 搭建Git本地服务器
- 【水水】HDU5665Lucky
- UVA 815 Flooded!
- mysql服务性能优化—my.cnf配置说明详解(16G内存)
- Android RGB颜色查询对照表