<poj-1258>Agri-Net
来源:互联网 发布:淘宝男装品牌折扣店 编辑:程序博客网 时间:2024/05/19 05:38
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 54352 Accepted: 22554
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
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28
解题思路
这个题就是个简单的最小生成树的题
就是也没有什么需要注意的地方就是直接套用模板就行了
#include<stdio.h>#include<string.h>#define MAX 0x3f3f3f3fint map[110][110];int vis[110];int dis[110];int sum;void prim(int n){ int min, minin; dis[1] = 0; for(int i = 1; i <= n; i++) { min = MAX; for(int j = 1; j <= n; j++) { if(vis[j] == 0&&dis[j] < min&&dis[j]!=-1) { min = dis[j]; minin = j; } } vis[minin] = 1; sum = sum + min; for(int j = 1; j <= n; j++) { if(vis[j]==0&&(dis[j] == -1||dis[j] > map[minin][j])) { dis[j] = map[minin][j]; } } }}int main(){ int n; while(scanf("%d",&n)!=EOF) { memset(map, 0, sizeof(map)); memset(vis, 0, sizeof(vis)); memset(dis, -1, sizeof(dis)); sum = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d",&map[i][j]); } } prim(n); printf("%d\n",sum); } return 0;}
0 0
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj 1258-Agri-net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ+1258++Agri-Net
- Poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj-1258-Agri-Net
- poj-1258 Agri-Net
- Poj 1258 Agri-Net
- POJ 1258 - Agri-Net
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- 过滤url 里参数
- WebService——wsdl文档深入分析
- 如何截取成只含年月日的日期?不懂的新手看看哦
- infoQ的文章
- <poj-1258>Agri-Net
- railgun项目的不足和将来会逐步新增的模块
- 王朝第十二周宏定义问题处理
- android使用Gradle统一配置依赖版本
- spring日志添加
- Java 5种字符串拼接方式性能比较
- C-C++练习11
- Nginx性能配置优化
- 事务