博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(最小生成树)Agri-Net -- POJ -- 1258
阅读量:7053 次
发布时间:2019-06-28

本文共 2308 字,大约阅读时间需要 7 分钟。

链接:

 

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45751   Accepted: 18835

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

40 4 9 214 0 8 179 8 0 1621 17 16 0

Sample Output

28

 

代码:

#include 
#include
#include
#include
using namespace std;const int N = 110;const int INF = 0xfffffff;int n, J[N][N], dist[N], vis[N];int Prim(){ int i, j, ans=0; dist[1]=0; memset(vis, 0, sizeof(vis)); vis[1]=1; for(i=1; i<=n; i++) dist[i]=J[1][i]; for(i=1; i
J[index][j]) dist[j]=J[index][j]; } } return ans;}int main (){ while(scanf("%d", &n)!=EOF) { int i, j; memset(J, 0, sizeof(J)); for(i=1; i<=n; i++) for(j=1; j<=n; j++) scanf("%d", &J[i][j]); int ans=Prim(); printf("%d\n", ans); } return 0;}

 

转载于:https://www.cnblogs.com/YY56/p/4735114.html

你可能感兴趣的文章
Android开发在路上:少去踩坑,多走捷径【转】
查看>>
动态代理模式
查看>>
将博客搬至CSDN
查看>>
JQuery 修改 form 表单的 action 的值,并提交
查看>>
IOS 百度地图导入最新 SDK 2.9 报错
查看>>
Spark快速入门指南
查看>>
MySql用navcat连接时报错 2509
查看>>
android 显示 网络图片
查看>>
安装MySQLdb模块-python
查看>>
ubuntu快捷键
查看>>
IOS——生成智能调试输出
查看>>
杀毒软件Avast被曝严重的0day漏洞
查看>>
NDK Caused by: java.lang.UnsatisfiedLinkError:
查看>>
oracle timestamp相减
查看>>
【swing】 BoxLayout布局
查看>>
Android 属性动画(Property Animation)完全解析 (下)
查看>>
GC overhead limit exceeded
查看>>
JDBC学习之三
查看>>
CSS3 渐变(Gradients)
查看>>
Windows7关机、重启、待机、休眠命令
查看>>