POJ1258 Agri-Net 2017-04-14 15:51 55人阅读 评论(0) 收藏
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 56948 | Accepted: 23615 |
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
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
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
Source
——————————————————————————————————————
#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
#define LL long long struct node
{
int u,v,w;
} p[1000005];
int n,cnt,pre[1005]; bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<1005; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} void kruskal()
{
sort(p,p+cnt,cmp);
init();
int cost=0;
int ans=0;
for(int i=0; i<cnt; i++)
{
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
}
if(ans==n-1)
{
break;
}
}
printf("%d\n",cost);
} int main()
{
int mp[105][105],k,x,y;
while(~scanf("%d",&n)&&n)
{ for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d",&mp[i][j]);
cnt=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
p[cnt].u=i,p[cnt].v=j;
p[cnt++].w=mp[i][j];
}
kruskal();
}
return 0;
}
POJ1258 Agri-Net 2017-04-14 15:51 55人阅读 评论(0) 收藏的更多相关文章
- C#控制管理VisualSVN Server 分类: C# 2014-05-29 15:51 796人阅读 评论(0) 收藏
VisualSVN Server可以用WMI接口管理(Windows Management Instrumentation). VisualSVN Server安装的计算机中,位于%VISUALSVN ...
- DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- 安装hadoop1.2.1集群环境 分类: A1_HADOOP 2014-08-29 15:49 1444人阅读 评论(0) 收藏
一.规划 (一)硬件资源 10.171.29.191 master 10.173.54.84 slave1 10.171.114.223 slave2 (二)基本资料 用户: jediael 目录 ...
- PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- 苹果应用商店AppStore审核中文指南 分类: ios相关 app相关 2015-07-27 15:33 84人阅读 评论(0) 收藏
目录 1. 条款与条件 2. 功能 3. 元数据.评级与排名 4. 位置 5. 推送通知 6. 游戏中心 7. 广告 8. 商标与商业外观 9. 媒体内容 10. 用户界面 11. 购买与货币 12. ...
- Codeforces816A Karen and Morning 2017-06-27 15:11 43人阅读 评论(0) 收藏
A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...
- Hadoop常见异常及其解决方案 分类: A1_HADOOP 2014-07-09 15:02 4187人阅读 评论(0) 收藏
1.Shell$ExitCodeException 现象:运行hadoop job时出现如下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : at ...
随机推荐
- redhat 防火墙禁止允许的IP链接指定的端口
编辑:iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -s 192.168.4.3 --dport 80 -j A ...
- https请求失败,解决方法
把请求头中 Content-Type 属性去掉就可以了
- CFGym 101161I 题解
一.题目链接 http://codeforces.com/gym/101161/problem/I 二.题意 给定一棵树,一个初始的省会城市,若干个询问,0表示修改省会城市,1表示查询去省会必须经过指 ...
- 7_python之路之python计算器
7_python之路之python计算器 1.程序说明:Readme.cmd 1.程序文件及说明: calculator.py 2.python版本:python-3.5.3 3.程序使用:pytho ...
- 适应移动端 iPhone & Android 微信页面的一些css属性
1.-webkit-tap-highlight-color -webkit-tap-highlight-color:rgba(0,0,0,0);//透明度设置为0,去掉点击链接和文本框对象时默认的灰色 ...
- requests and BeautifulSoup
requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...
- uva-10815-字符串排序
又偷懒了,字符串排序,贱贱的用了std:map #include <iostream> #include <sstream> #include<algorithm> ...
- windows 下 YII2 配置 memcache
环境: 操作系统 :Windows 7; php: 5.6.8 apche:2.4.12 1.首先安装PHP memcache 拓展,安装方法如下: 1.1下载 memcache 拓展DLL: ht ...
- 关于javascript中的typeof和instanceof介绍
typeof用来检测给定变量的数据类型 instanceof用来检测对象的类型 typeof用来检测给定变量的数据类型(也可叫做基本类型,基本数据类型.包含undefined.boolean.stri ...
- C++全总结
// CPPTEST.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include & ...