uvaoj 10397 - Connect the Campus【最小生成树】
uvaoj 10397 - Connect the Campus
Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is connected to every other building (directly or indirectly) through the campus network of communication cables. We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings). You have been given a campus map which shows the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of new cable that you use.
Input
The input file describes several test cases. The description of each test case is given below: The first line of each test case contains the number of buildings N (1 ≤ N ≤ 750). The buildings are labeled from 1 to N. The next N lines give the x and y coordinates of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 ≤ M ≤ 1000) followed by M lines describing the existing cables. Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.
Output
For each set of input, output in a single line the total length of the new cables that you plan to use rounded to two decimal places.
Sample Input
4
103 104
104 100
104 103
100 100
1
4 2
Sample Output
4.41
题意:给你一个数字n表示建筑的数量,接下来n行是每座建筑的坐标,然后一个数字m接下来m行每行两个数a,b,表示建筑a和建筑b之间已经联通,问联通n个建筑的最短距离
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define INF 0x3f3f3f
#define DD double
#define MAX 1010
using namespace std;
int n,m;
int sum;
DD b[MAX],a[MAX];
DD map[MAX][MAX];
int vis[MAX];
DD low[MAX];
DD fun(int i,int j)
{
return sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));//求两个城市之间的距离
}
void init()
{
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=i==j?0:INF;
}
int main()
{
int t,i,j,k;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=1;i<=n;i++)
scanf("%lf%lf",&a[i],&b[i]);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
map[i][j]=map[j][i]=fun(i,j);
}
} scanf("%d",&m);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
map[x][y]=map[y][x]=0;//已经联通的城市距离为0
}
int next;
DD min,mindis=0;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
low[i]=map[1][i]; vis[1]=1;
for(i=1;i<n;i++)
{
min=INF;
next=1;
for(j=1;j<=n;j++)
{
if(min>low[j]&&!vis[j])
{
next=j;
min=low[j];
}
} mindis+=min;
vis[next]=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&low[j]>map[next][j])
low[j]=map[next][j];
}
}
printf("%.2lf\n",mindis);
}
return 0;
}
uvaoj 10397 - Connect the Campus【最小生成树】的更多相关文章
- UVa 10397 Connect the Campus
最小生成树 Kruskal #include<cmath> #include<iostream> #include<cstdio> #include<algo ...
- UVa10397_Connect the Campus(最小生成树)(小白书图论专题)
解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <ios ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)
题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- zoj 3204 Connect them(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
随机推荐
- Android中图片的异步加载
转: 1. 为什么要异步加载图片 下载图片比较费时,先显示文字部分,让加载图片的过程在后台,以提升用户体验 2. SoftReference的作用 栈内存—引用 堆内存—对象 Eg: Object ...
- 关于datagridview单元格不切换焦点无法获得新输入数据的问题解决方法
问题描述:将EXCEL中的数据导入到dataGridView中,然后通过点击toolStripButton对dataGrideView中的数据进行处理,在测试时,向dataGridView中的某个单元 ...
- php发送http请求
http请求有get,post. php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]. file_get_contents();详情见:http://www.cnblogs.com/ ...
- eval 如何定义函数
eval(compile('''def fun(): print 'bbb' ''', '<string>', 'exec')) fun()
- lucene开发序之luke神器
lucene是一款很优秀的全文检索的开源库,目前最新的版本是lucene4.4,关于lucene的历史背景以及发展状况,在这里笔者就不多介绍了,如果你真心想学习lucene,想必在这之前你已经对此作过 ...
- 2016031601 - 刻录ubuntu的U盘启动盘
使用UltraISO来刻录ubuntu15.1的系统安装盘. 需要U盘1个,ubuntu15.1系统镜像文件. 具体步骤如下: a.安装UltraISO软件,具体软件安装. b.安装完成后进行系统刻录 ...
- php自学笔记1
PHP(Personal Home Page的缩写,现已更名Hypertext Preprocessor“超文本预处理器”)静态web开发:html,内容一成不变动态web开发: php(Zend). ...
- JavaScript的OOP编程1
首先要说的是,javascript其实是可以进行OOP编程的,其次javascript的OOP编程实现方式有多种,我写的这一种只是我测试过,可行的一种 version1 // 父类 function ...
- JVM 学习笔记(二)
JVM 堆中几乎存放着java中所有的对象实例,在在垃圾回收前先要判断对象是否已死,这里对对象的判断主要有: 1. 引用计数法 给对象中添加一个引用计数器,每当有一个地方引用他时,计数器就加1:当引 ...
- C#学习笔记一:C#开发环境的设置
C#是.NET Framework的一部分,用于编写.NET应用程序. C#集成开发环境(IDE) 微软提供了以下C#编程开发工具: Visual Studio 2010 (VS) Visual C# ...