UVa 10397 Connect the Campus
最小生成树
Kruskal
#include<cmath>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; struct A{
int x,y;
double z;
}a[500010];
int r[755];
int find_(int x)
{
while(x!=r[x])
x=r[x];
return x;
} int cmp(A a,A b)
{
return a.z<b.z;
}
int main()
{
int n,m,i,j,k,c,b,x[755],y[755];
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
for(i=1;i<=n;i++)
r[i]=i;
k=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
if(i>j){
double s=sqrt( (x[i]-x[j])*(x[i]-x[j])+ (y[i]-y[j])*(y[i]-y[j]) );
a[++k].x=i;
a[k].y=j;
a[k].z=s;
}
}
scanf("%d",&m);
while(m--){
scanf("%d%d",&c,&b);
int aa=find_(c);
int bb=find_(b);
if(aa!=bb){
r[aa]=bb;
}
} sort(a+1,a+1+k,cmp);
double ans=0;
for(i=1;i<=k;i++)
{
int aa=find_(a[i].x);
int bb=find_(a[i].y);
if(aa!=bb){
r[aa]=bb;
ans=ans+a[i].z;
}
}
printf("%.2lf\n",ans); }
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
UVa 10397 Connect the Campus的更多相关文章
- uvaoj 10397 - Connect the Campus【最小生成树】
uvaoj 10397 - Connect the Campus Many new buildings are under construction on the campus of the Univ ...
- Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)
题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- UVa10397_Connect the Campus(最小生成树)(小白书图论专题)
解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <ios ...
- UVA 1456 六 Cellular Network
Cellular Network Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit S ...
- uva 315 Network(无向图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【UVA 1411】 Ants (KM)
Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- uva 1411 Ants (权值和最小的完美匹配---KM算法)
uva 1411 Ants Description Young naturalist Bill studies ants in school. His ants feed on plant-louse ...
随机推荐
- Yii/Yii2:查询返回以某特定列为索引(键)的数组
在Yii1.x中,要想查询语句返回以特定列为键(key)的数组数据,代码例如以下(下述样例以表主键为返回数组索引): $users = User::model()->findAll(array( ...
- 理解RESTful架构(转)
越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...
- Python学习路径8——Python对象2
1.标准型运营商 1.1对象值对照 比较运算符用于如果相同类型的对象是相等.所有的内建类型的是在比较操作中支持,返回布尔比较操作值True 或 False. <span style=" ...
- LAMP一键安装包(Python版)
去年有出一个python整的LAMP自动安装,不过比较傻,直接调用的yum 去安装了XXX...不过这次一样有用shell..我也想如何不调用shell 来弄一个LAMP自动安装部署啥啥的..不过尼玛 ...
- C# Windows Phone 8 WP8 开发,将WebClient的DownloadStringCompleted事件改成非同步的awiat方法。
原文:C# Windows Phone 8 WP8 开发,将WebClient的DownloadStringCompleted事件改成非同步的awiat方法. 一般我们在撰写Windows Phone ...
- W5500 keep-alive的用途及使用
大家是否遇到过这种问题,W5500作为server已经建立连接,突然网线掉了,然后再去连接W5500.就连不上了. 为什么?以下对这个问题进行解释说明,并提出解决的方法. 图1中的上位机程序作为cli ...
- DateTime.Compare(t1,t2)比較两个日期大小
DateTime.Compare(t1,t2)比較两个日期大小,排前面的小,排在后面的大,比方:2011-2-1就小于2012-3-2返回值小于零: t1 小于 t2. 返回值等于零 : t1 等于 ...
- shell文字过滤程序(十):cut命令
[版权声明:转载请保留源:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] cut指挥类似至awk,从行内提取的信息,它是一个较弱的版本的功能 ...
- 【原创】leetCodeOj --- Excel Sheet Column Title 解题报告
题目地址: https://oj.leetcode.com/problems/excel-sheet-column-title/ 题目内容: Given a positive integer, ret ...
- Aix db2 经user a using b连接时报SQL30082N Security processing failed with reason "42"
db2inst1登录 输入实例文件夹:/opt/ibm/db2/V9.7/instance 关闭实例:db2stop 更新示例:./db2iupdt db2inst1 启动实例:db2start 再次 ...