博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1258 -- Agri-Net
阅读量:6379 次
发布时间:2019-06-23

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

Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 38406   Accepted: 15469

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 水题一道。最小生成树。prim即可
1 /*====================================================================== 2  *           Author :   kevin 3  *         Filename :   AgriNet.cpp 4  *       Creat time :   2014-07-09 15:55 5  *      Description : 6 ========================================================================*/ 7 #include 
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define clr(a,b) memset(a,b,sizeof(a))14 #define M 20015 #define INF 0x7f7f7f7f16 using namespace std;17 int c[M][M],dis[M];18 int prim(int n)19 {20 bool vis[M];21 int i,j,k,sum = 0;22 for(i = 0; i < n; i++){23 dis[i] = c[0][i];24 vis[i] = false;25 }26 vis[0] = true;27 for(i = 1; i < n; i++){28 int _min = INF;29 j = 0;30 for(k = 0; k < n; k++){31 if(_min > dis[k] && !vis[k]){32 _min = dis[k];33 j = k;34 }35 }36 vis[j] = true;37 sum += dis[j];38 for(k = 0; k < n; k++){39 if(dis[k] > c[j][k] && !vis[k]){40 dis[k] = c[j][k];41 }42 }43 }44 return sum;45 }46 int main()47 {48 int n;49 while(scanf("%d",&n)!=EOF){50 clr(c,0);51 int value;52 for(int i = 0; i < n; i++){53 for(int j = 0; j
View Code

 

转载于:https://www.cnblogs.com/ubuntu-kevin/p/3833779.html

你可能感兴趣的文章
深入探索AngularJS(持续更新)
查看>>
程序员的10大成功面试技巧
查看>>
Java cxf WebService 入门[sayHello]
查看>>
一个线程的独白
查看>>
elasticsearch threadpool setting
查看>>
http协议content-encoding & transfer-encoding
查看>>
二叉树——BinaryTree 非递归遍历算法(Java)
查看>>
iphone:给任意的控件进行截图
查看>>
ubuntu 13.04 安装 gitlab 5.3 版
查看>>
Xqk.Data数据框架开发指南:丰富的、灵活的查询方法(第二部分:适应不同数据库系统的查询)...
查看>>
linux Svn服务器安装
查看>>
PHP连接局域网MYSQL数据库的简单实例
查看>>
Android Studio下Jni开发配置
查看>>
wdCP v3正式版发布
查看>>
学习jQuery必须知道的几种常用方法
查看>>
CSS3 背景
查看>>
php7.2安装zookeeper扩展
查看>>
SQL 查询语句中in与not in查出来的条数不是互补的
查看>>
nosql
查看>>
Fiddler2 中文手册“无法显示网页”问题的解决办法
查看>>