最小生成树

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的更多相关文章

  1. uvaoj 10397 - Connect the Campus【最小生成树】

    uvaoj 10397 - Connect the Campus Many new buildings are under construction on the campus of the Univ ...

  2. Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)

    题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...

  3. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  4. UVa10397_Connect the Campus(最小生成树)(小白书图论专题)

    解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <ios ...

  5. UVA 1456 六 Cellular Network

    Cellular Network Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  6. uva 315 Network(无向图求割点)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. 【UVA 1411】 Ants (KM)

    Young naturalist Bill studies ants in school. His ants feed onplant-louses that live on apple trees. ...

  8. 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 / ...

  9. uva 1411 Ants (权值和最小的完美匹配---KM算法)

    uva 1411 Ants Description Young naturalist Bill studies ants in school. His ants feed on plant-louse ...

随机推荐

  1. hdu4857 逃生 bestcoder round1 A

    题目要求要求在满足约束条件的情况下,使小的序号尽力靠前. 坑点就在这里.小的序号尽量靠前并非代表字典序,它要求多种情况时,先使1靠前(可能1仅仅能在第2或第3位 那么就要使它在第2位),其次2,3. ...

  2. c# 通过解析mp3规范命名并上传服务器

    引用using Shell32; private void Open_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = ne ...

  3. [Web Chart系列之五] 6. 实战draw2d之ConnectionRouter

    前言 ConnectionRouter 的作用是定义连线的展示样式. 是直线连接还是曲线连接(好像也是基于Bezier曲线) 位于包: draw2d.layout.connection 下. 常见的有 ...

  4. 一旦ORA-28000: the account is locked用户锁定故障排除

    今天我的同事们反映的问题,测试库的变化password,并改变相关的应用程序中使用password后,其中一个仍然会出现在帐户被锁定,报告ORA-28000: the account is locke ...

  5. 泛型Binary Search Tree实现,And和STL map比较的经营业绩

    问题叙述性说明: 1.binary search tree它是一种二进制树的.对于key值.比当前节点左孩子少大于右子. 2.binary search tree不是自平衡树.所以,当插入数据不是非常 ...

  6. QQ旅程server分析01-网关server

    网关server网络进程: s1.连接网关管理器 s2.上报自身信息给网关管理器 s3.从网关管理器获取自己的管理信息 *s4.依照管理信息预分配好须要的资源 s5.网关server打开client监 ...

  7. 初步C++类模板学习笔记

    类模板 实现:在上课时间的定义给它的一个或多个参数,这些参数代表了不同的数据类型.                              -->抽象的类. 在调用类模板时, 指定參数, 由编 ...

  8. hdu1372 dfs搜索之国际象棋的马

    原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到还有一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四 ...

  9. go语言实现遍历目录,及查找特定的文件类型

    // filelist.go package main import ( //"flag" "fmt" "os" "path/fi ...

  10. Binomial Coeffcients 历届山东省省赛题

    Binomial Coeffcients Time Limit: 1000MS Memory limit: 65536K 题目描述   输入   输出   示例输入 3 1 1 10 2 954 72 ...