Agri-Net

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem 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
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
 
Sample Output
28
 #include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;
int e[][];
int d[];
bool v[];
int main()
{
int n, i, j;
while (cin >> n)
{
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
cin >> e[i][j];
}
}
for (i = ; i <= n; i++)
{
d[i] = e[][i];
}
memset(v, , sizeof(v));
v[] = ;
int s = ;
for (i = ; i <= n - ; i++)
{
int k = -;
int mi = ;
for (j = ; j <= n; j++)
{
if (!v[j] && d[j] <= mi)
{
mi = d[j];
k = j;
}
}
if (k == -) break;
v[k] = ;
s = s + d[k];
for (j = ; j <= n; j++)
{
if (!v[j] && d[j] > e[k][j])//这一步和dijkstra不太同,不是d[j]>d[k]+e[k][j]
{
d[j] = e[k][j];
}
}
}
cout << s << endl;
}
return ;
}

最新文章

  1. BZOJ 1111: [POI2007]四进制的天平Wag
  2. 样条曲线的Fortran程序
  3. adMob iAd整合,随机根据网络状况自动显示。
  4. SQL 跟踪方法相关介绍
  5. SNMP监控一些常用OID的总结
  6. laravel homestead
  7. obj.onclick=fnClick与obj.onclick=fnClick()的区别
  8. 网络协议- HTTP
  9. FairScheduler的任务调度机制——assignTasks
  10. HTML5学习+javascript学习:打飞机游戏简介以及Model层
  11. 设计模式——外观模式(C++实现)
  12. 用HTML5+原生js实现的推箱子游戏
  13. VS Code 安装 C++ 调试环境
  14. 单用户实例添加DB账号
  15. consul介绍
  16. mysql半同步开启
  17. hdu 4746Mophues[莫比乌斯反演]
  18. USB 转LAN AX88772B 模块驱动添加记录
  19. 【Alpha】Phylab 发布说明
  20. 树dp...吧 ZOJ 3949

热门文章

  1. java中static关键字的使用
  2. linux突然断电重启,配置文件丢失/程序无法打开/文件损坏
  3. Java IO流-标准输入输出流
  4. PHP如何生成文章预览图
  5. telent服务搭建并远程连接
  6. bzoj-4887-dp+矩阵快速幂
  7. zoj-3329-期望/dp/方程优化
  8. Life Cycle(JSF+Facelets)
  9. PHPExcel解决内存占用过大问题-设置单元格对象缓存
  10. leetcode122 买卖股票的最佳时机 python