POJ:1258-Agri-Net
Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 65322 Accepted: 27029
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 <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 110;
int n,father[maxn],tot;
struct Path {
int s,e,len;
}path[maxn*maxn];
int find(int x) {
if(x == father[x])
return x;
return father[x] = find(father[x]);
}
void merge(int x,int y) {
int fx = find(x);
int fy = find(y);
father[fx] = fy;
}
bool cmp(Path a,Path b) {
return a.len < b.len;
}
void init() {
tot = 0;
for(int i=0;i<n;i++) {
father[i] = i;
for (int j = 0; j < n; j++) {
int temp;
scanf("%d", &temp);
path[tot].s = i;
path[tot].e = j;
path[tot++].len = temp;
}
}
sort(path,path+tot,cmp);
}
int main() {
while(scanf("%d",&n) != EOF) {
init();
int ans = 0;
for(int i=0;i<tot;i++){
if(find(path[i].s) != find(path[i].e)) {
ans += path[i].len;
merge(path[i].s,path[i].e);
}
}
printf("%d\n",ans);
}
return 0;
}
POJ:1258-Agri-Net的更多相关文章
- poj:4091:The Closest M Points
poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...
- POJ:1182 食物链(带权并查集)
http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...
- POJ:2229-Sumsets(完全背包的优化)
题目链接:http://poj.org/problem?id=2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissio ...
- POJ:3126-Prime Path
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)
原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...
- POJ :3614-Sunscreen
传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ:2139-Six Degrees of Cowvin Bacon
传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...
- POJ:1017-Packets(贪心+模拟,神烦)
传送门:http://poj.org/problem?id=1017 Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ:3190-Stall Reservations
传送门:http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- python处理字符串:将字符串中的数字相加求和
计算字符串中所有数字的和,字符串中有数字和字母组合而成如果出现连续数字,按照一个数操作具体解释在代码行里: def sum_str(str1): len1=len(str1) #首先将字符串str1的 ...
- 【工作中学习】CreateProcessAsUser失败,错误码:1314
事情起因是这样, 产品的Windows服务(Service)之前一直是用Local System Account在运行的,但这个版本有需求要换成使用普通的Domain User来运行,如下图: 但却出 ...
- Mac 下显示隐藏文件或文件夹
Mac 操作系统 隐藏显示文件 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:defaults write co ...
- Struts2_带参数的结果集
页面请求: <a href="user/user?type=1">传参数</a> action: public Integer type; public S ...
- 监控系统 - pnp4nagios
pnp4nagios是nagios的一个插件,用于将perfdata数据写入rrd,用于展示流量图,目前最高版本0.6.25. 我用官方下载的tar.gz打rpm包,官方提供的pnp4nagios.s ...
- ansible使用3-playbook
playbook是ansible用于配置部署的语言.使用YAML格式. 示例 --- - hosts: webservers vars: http_port: 80 max_clients: 200 ...
- yii网站未来改进
1.去掉debug模式 2.下载文件复选 3.文章发布.评论.赞.标签等系统
- Java项目性能瓶颈分析及定位(八)——Java线程堆栈分析(五)
对于CPU而言,常见的瓶颈主要有两种:服务器的压力很小,但是CPU的利用率却很高,这样的性能瓶颈相对比较容易定位(好比我只是说了你一句,你就哭了,你的弱点立马就暴露出来了):给服务器施加的压力很大,但 ...
- 洛谷 P2814 家谱
题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. 输入输出格式 输入格式: 输入由多行组成,首先是一系列有关父子关系的描述,其中每一组 ...
- 查看Linux网卡地址,网络地址
查看网络地址 ip a 或ip addr show 或ifconfig,此指令在部分linux系统中不支持