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算法就可以了:我没 ...
随机推荐
- mysql 事务类型表的用法
mysql关联表(references)的条件:1.两个表必须是 InnoDB表类型2.使用在外键关系的域必须为索引型(Index)3.使用外键关系的域必须与数据类型相似 以下是父表和子表的例子:创建 ...
- C#事件作用和用法
例如有下面的需求需要实现:程序主画面中弹出一个子窗口.此时主画面仍然可以接收用户的操作(子窗口是非模态的).子窗口上进行某些操作,根据操作的结果要在主画面上显示不同的数据. 即如下图所示: 大多数我们 ...
- VS建立可供外部调用的MFC类DLL,C#调用MFC调用
建立MFC DLL工程.一般选共享MFC库 关键是在你生成的CPP中,添加外部调用的接口 如下,意思是将这个函数对外公开. 如果你希望对外提供类,就把这个方法做成工厂. 如果你希望对外提供MFC的窗体 ...
- wdcp日志
apache或nginx都有开关默认日志,一个是正常访问日志,一个是错误的日志,目录在 /www/wdlinux/nginx-1.0.15/logs /www/wdlinux/httpd-2.2.22 ...
- JBPM4 常用表结构及其说明
本文从表结构.操作时表的变化以及jbpm4.4各个包的作用来介绍jbpm的. 第一部分:表结构说明 Jbpm4 共有18张表,如下,其中红色的表为经常使用的表 一:资源库与运行时表结构 1. J ...
- Xcode6 Xcode7 Xcode 官方链接 --备用
Xcode 6 官方下载链接: http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/xcode_6_beta.dmg Xcode ...
- C++中二维数组的动态创建与处理
C++中用new动态创建二维数组的格式一般是这样: TYPE (*p)[N] = new TYPE [][N]; 其中,TYPE是某种类型,N是二维数组的列数.采用这种格式,列数必须指出,而行数无需指 ...
- css实现网页布局随滚轮变化响应移动
_position:absolute; _top:expression(eval(document.documentElement.scrollTop)); 1.第一句代码 _position:abs ...
- 【HDOJ】2955 Robberies
01背包.将最大金额作为容量v.概率做乘法. #include <stdio.h> #include <string.h> #define mymax(a, b) (a> ...
- git从指定的commit创建分支
How do I create a new git branch from an old commit? git checkout -b justin a9c146a09505837ec03b Thi ...