POJ 1258 Agri-Net (prim水题)
Agri-Net
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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.
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.
the sum of the minimum length of fiber required to connect the entire set of
farms.
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
#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 ;
}
POJ 1258 Agri-Net (prim水题)的更多相关文章
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
- poj 1004:Financial Management(水题,求平均数)
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126087 Accepted: ...
- POJ 3176 Cow Bowling (水题DP)
题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...
- vijos 1069 新年趣事之红包 Prim水题
描述 xiaomengxian一进门,发现外公.外婆.叔叔.阿姨……都坐在客厅里等着他呢.经过仔细观察,xiaomengxian发现他们所有人正好组成了一个凸多边形.最重要的是,他们每个人手里都拿着一 ...
- poj 1658 Eva's Problem(水题)
一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- POJ 1258 Agri-Net (最小生成树+Prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39820 Accepted: 16192 Descri ...
随机推荐
- Windows自带计算器快捷键
今天乱翻的时候发现了这个东西,下面就是各个快捷键: (以下功能在计算器面板上均能找到) 按键 功能 F9 \(-/+\) R 1/x @ \(\sqrt{}\) Ctrl+Shift+D 清除历史记录 ...
- MyBatis使用小案例
首先回顾一下MyBatis封装简化Dao层连接数据库操作的顺序. 首先MyBatis是一个引入的jar包,还有一些依赖包,可能用不到的jar包,一并引入就好了,再多引入一个Juntil.jar测试包( ...
- 【eclipse】点Clean后没反应
问题:点击Clean重新编译class后没反应 解决:
- css3 导入字体
在CSS中导入字体或是字体ICON @font-face{ src: url("具体的字体地址"), url("具体的字体地址"); }
- shell fold限制文件行宽
将文本的行限制到特定的宽 这个用 echo 命令发送的文本用 -w 选项分解成块. 在这个例子中,我们设定了行宽为12个字符. 如果没有字符设置,默认是80. 增加的 -s 选项将让 fold 分解到 ...
- Qt5获取网卡/IP等信息
参考网址:http://blog.csdn.net/wjs1033/article/details/22697063 1.环境 Win7x64.Qt5.5.1(x86).vs2013_ultimate ...
- 雷林鹏分享:Ruby Socket 编程
Ruby Socket 编程 Ruby提供了两个级别访问网络的服务,在底层你可以访问操作系统,它可以让你实现客户端和服务器为面向连接和无连接协议的基本套接字支持. Ruby 统一支持应用程的网络协议, ...
- Learn Rails5.2- ActiveRecord: sqlite3的用法, Query查询语法。乐观锁和悲观锁案例,查询语法includes(), 多态关联,destory和delete, Scope, Validats, Migrations
rails generate model photo title:string album:references 这会产生一个album_id列,当建立belongs_to关联时,需要用到. refe ...
- mysql安装不上怎么办 mysql安装失败原因和解决方法
困难1:MySQL 5.1 安装过程中报apply security setting错误 1.卸载MySQL. 2.删除目录 C:\Documents and Settings\All Users\A ...
- CMD中goto语句会中断for循环特性详解
在这个程序里面由于用到了上篇文章中所说的字符串切割,而用到了Goto强制跳转语句 但是在程序中使用的时候却发现一个错误,当把这个字符切割的代码段如果直接作为非嵌套语句执行正常 但是一旦放到for循环的 ...