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 ...
随机推荐
- 【白银组】codevs_1160 蛇形矩阵
#include <iostream> using namespace std; #define M 100 int a[M][M]; void pt( int n ) { for ( i ...
- Java 权限框架 Shiro 实战一:理论基础
Apache Shiro 官网地址:http://shiro.apache.org/ Apache Shiro is a powerful and easy-to-use Java security ...
- [转]Acrylic DNS Proxy 使用方法
本文转自:http://www.cnwyw.net/index.php/acrylic-dns-proxy-ping-bi-guang-gao/ 从开始菜单进行“Edit Configuration ...
- Hive 体系结构
1.Hive架构与基本组成 下面是Hive的架构图. 图1.1 Hive体系结构 Hive的体系结构可以分为以下几部分: (1)用户接口主要有三个:CLI,Client 和 W ...
- javascript的创建对象object.create()和属性检测hasOwnPrototype()和propertyIsEnumerable()
Object.create("参数1[,参数2]")是E5中提出的一种新的对象的创建方式. 第一个参数是要继承到新对象原型上的对象; 第二个参数是对象属性.这个参数可选,默认为fa ...
- SPM——How to use github
In this semester, we take a class called 'Software Project Management'. And in this class, we have l ...
- 206. Reverse Linked List + 92. Reverse Linked List II
▶ 关于单链表翻转的两个问题. ▶ 206. 翻转整个单链表. ● 自己的代码,9 ms,使用了递归. class Solution { public: ListNode* reverseList(L ...
- leetcode119
public class Solution { public IList<int> GetRow(int rowIndex) { List<List<int>> l ...
- 当前触发事件的两种方式(onclick) 和 ('id') 获取
1. <input type='text' onclick = 'Clickon(this)'> <script> function Clickon(num){ num.sty ...
- 12 MySQL--内置功能介绍
mysql内置功能: 1.视图 2.触发器 3.存储过程 4.事务 5.函数 6.函数流程控制 一.视图 介绍: 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名] ...