POJ1258:Agri-Net(最小生成树模板题)
http://poj.org/problem?id=1258
Description
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
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28 题目大意:有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离
解题思路:又是一个最小生成树,因为给出了一个二维矩阵代表他们的距离,直接算prim就行了。
#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
int map[][];
int n,dis[],v[];
void prim()
{
int min,sum=,k;
for(int i=; i<=n; i++)
{
v[i]=;
dis[i]=INF;
}
for(int i=; i<=n; i++)
dis[i]=map[][i];
v[]=;
for(int j=; j<n; j++)
{
min=INF;
for(int i=; i<=n; i++)
{
if(v[i]==&&dis[i]<min)
{
k=i;
min=dis[i];
}
}
sum+=min;
v[k]=;
for(int i=; i<=n; i++)
{
if(v[i]==&&map[k][i]<dis[i])
{
dis[i]=map[k][i];
}
}
}
cout<<sum<<endl;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
cin>>map[i][j];
}
}
prim();
}
return ;
}
POJ1258:Agri-Net(最小生成树模板题)的更多相关文章
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- O - 听说下面都是裸题 (最小生成树模板题)
Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...
- 最小生成树模板题-----P3366 【模板】最小生成树
题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- 最小生成树模板题POJ - 1287-prim+kruskal
POJ - 1287超级模板题 大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度? 这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出 ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ 1287 Networking (最小生成树模板题)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- 继续畅通工程--hdu1879(最小生成树 模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1879 刚开始么看清题 以为就是n行 后来一看是n*(n-1)/2行 是输入错误 真是够够的 #incl ...
- 最小生成树模板题 hpu 积分赛 Vegetable and Road again
问题 H: Vegetable and Road again 时间限制: 1 Sec 内存限制: 128 MB 提交: 19 解决: 8 题目描述 修路的方案终于确定了.市政府要求任意两个公园之间都必 ...
随机推荐
- thinkphp3.2 实现留言功能
写一个例子说明一下: 前端:http://www.mmkb.com/zhendao/index/feedback.html <form method="post" actio ...
- springbatch---->springbatch的使用(五)
这里我们介绍一个从数据库读取数据并写入到文件中的案例.如果能真心爱上一个人,那么不管对方是何等恶劣,哪怕对方并不爱自己,人生也至少不会是地狱,就算多少有点黯淡. 读取数据库数据 一.定义一个读写的jo ...
- git-常用命令一览表
一.git bash 操作文件 常用命令表 git常用 文件操作 命令 功能分类 命令 说明 备注 目录切换 cd 文件目录 改变/切换 目录, change directory 如 cd e:\ff ...
- gerrit_bash_commands.sh
https://github.com/tomwys/gerrit-bash-commands gerrit_bash_commands.sh # Author: Tomasz Wysocki < ...
- 国产手机插入mac os 系统中无法被识别的解决方法
一些国产手机插入mac os 系统中无法被识别,在命令行输入 system_profiler SPUSBDataType在, 然后将魅蓝note的vendor id 添加至 ~/.android/ad ...
- CF 455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Spring web.xml中的配置
转载博客:http://blog.163.com/zhangke_616/blog/static/191980492007994948206/ 在实际项目中spring的配置文件application ...
- Xcode - xcode-select: error: tool 'xcodebuild' requires Xcode报错解决方案
用mac 自带的终端执行的命令,安装安装Vapor和toolbox 安装指令: macdeMacBook-Pro:~ mac$ curl -sL check.vapor.sh| bash 结果报这个错 ...
- iOS - 使用苹果自带的UIVideoEditController进行视频编辑
UIVideoEditorController类包含了由系统提供的界面,使用户可以交互式的剪切视频.UIVideoEditorController对象处理用户的交互并且提供把编辑后的视频的文件系统路径 ...
- 经典 mysql 28道题
1.登陆MySQL数据库. mysql -uroot -pdadong123 2.查看当前登录的用户. select user(); select user from mysql.user; 3.创建 ...