POJ1258-Agri-Net-ACM
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
1 #include <stdio.h> 2 #include <string.h> 3 #define V 256 4 5 #define typec int 6 const typec inf = 0x7fffffff; 7 int vis[V]; 8 typec lowc[V]; 9 10 typec prim(typec cost[][V], int n) 11 { 12 int i, j, p; 13 typec minc, res = 0; 14 memset(vis, 0, sizeof(vis)); 15 vis[0] = 1; 16 for (i=1; i<n; i++) lowc[i] = cost[0][i]; 17 for (i=1; i<n; i++) { 18 minc = inf; p = -1; 19 for (j=0; j<n; j++) 20 if (0 == vis[j] && minc > lowc[j]) { 21 minc = lowc[j]; p = j; 22 } 23 if (inf == minc) return -1; 24 res += minc; vis[p] = 1; 25 for (j=0; j<n; j++) 26 if (0 == vis[j] && lowc[j] > cost[p][j]) 27 lowc[j] = cost[p][j]; 28 } 29 return res; 30 } 31 32 int matrix[V][V]; 33 34 int main(){ 35 int n,i,j; 36 while((scanf("%d",&n) == 1) && (n>=3 && n<=100) ){ 37 for(i=0;i<n;i++){ 38 for(j=0;j<n;j++){ 39 scanf("%d",&matrix[i][j]); 40 } 41 } 42 43 int res = prim(matrix,n); 44 if(-1 != res){ 45 printf("%d\n",res); 46 } 47 } 48 49 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)