UVa10397_Connect the Campus(最小生成树)(小白书图论专题)
解题报告
题意:
使得学校网络互通的最小花费,一些楼的线路已经有了。
思路:
存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,_hash[1110][1110],vis[1100];
double mmap[1110][1110],dis[1100];
struct node {
double x,y;
} p[1110];
double disc(node p1,node p2) {
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
void prime() {
for(int i=0; i<n; i++) {
dis[i]=mmap[0][i];
vis[i]=0;
}
double minn=(double )inf,ans=0;
int u;
dis[0]=0;
vis[0]=1;
for(int i=0; i<n-1; i++) {
minn=inf;
for(int j=0; j<n; j++) {
if(!vis[j]&&dis[j]<minn) {
minn=dis[j];
u=j;
}
}
ans+=minn;
vis[u]=1;
for(int j=0; j<n; j++) {
if(!vis[j]&&mmap[u][j]<dis[j]) {
dis[j]=mmap[u][j];
}
}
}
printf("%.2lf\n",ans);
}
int main() {
int i,j,u,v,w,k=1;
while(~scanf("%d",&n)) {
for(i=0; i<n; i++) {
for(j=0; j<n; j++)
mmap[i][j]=(double)inf;
mmap[i][i]=0;
}
for(i=0; i<n; i++) {
scanf("%lf%lf",&p[i].x,&p[i].y);
}
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
mmap[i][j]=disc(p[i],p[j]);
}
}
scanf("%d",&m);
while(m--) {
scanf("%d%d",&u,&v);
mmap[u-1][v-1]=mmap[v-1][u-1]=0;
}
prime();
}
return 0;
}
Connect the Campus
Input: standard input
Output: standard output
Time Limit: 2 seconds
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.
Fig: University of Waterloo Campus
Input
The input file describes several test case. 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 Nlines 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 byM 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
4
103 104
104 100
104 103
100 100
1
4 2
Sample Output
4.41
4.41
UVa10397_Connect the Campus(最小生成树)(小白书图论专题)的更多相关文章
- UVa10099_The Tourist Guide(最短路/floyd)(小白书图论专题)
解题报告 题意: 有一个旅游团如今去出游玩,如今有n个城市,m条路.因为每一条路上面规定了最多可以通过的人数,如今想问这个旅游团人数已知的情况下最少须要运送几趟 思路: 求出发点到终点全部路其中最小值 ...
- UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
解题报告 题意: n个插头m个设备k种转换器.求有多少设备无法插入. 思路: 定义源点和汇点,源点和设备相连,容量为1. 汇点和插头相连,容量也为1. 插头和设备相连,容量也为1. 可转换插头相连,容 ...
- UVa567_Risk(最短路)(小白书图论专题)
解题报告 option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=508"& ...
- UVa10048_Audiophobia(最短路/floyd)(小白书图论专题)
解题报告 题意: 求全部路中最大分贝最小的路. 思路: 类似floyd算法的思想.u->v能够有另外一点k.通过u->k->v来走,拿u->k和k->v的最大值和u-&g ...
- UVa563_Crimewave(网络流/最大流)(小白书图论专题)
解题报告 思路: 要求抢劫银行的伙伴(想了N多名词来形容,强盗,贼匪,小偷,sad.都认为不合适)不在同一个路口相碰面,能够把点拆成两个点,一个入点.一个出点. 再设计源点s连向银行位置.再矩阵外围套 ...
- UVa409_Excuses, Excuses!(小白书字符串专题)
解题报告 题意: 找包括单词最多的串.有多个按顺序输出 思路: 字典树爆. #include <cstdio> #include <cstring> #include < ...
- 正睿OI国庆DAY2:图论专题
正睿OI国庆DAY2:图论专题 dfs/例题 判断无向图之间是否存在至少三条点不相交的简单路径 一个想法是最大流(后来说可以做,但是是多项式时间做法 旁边GavinZheng神仙在谈最小生成树 陈主力 ...
- UVA 571 Jugs ADD18 小白书10 数学Part1 专题
只能往一个方向倒,如c1=3,c2=5,a b从0 0->0 5->3 2->0 2->2 0->2 5->3 4->0 4->3 1->0 1- ...
- Django框架详细介绍---ORM---图书信息系统专题训练
from django.db import models # Create your models here. # 书 class Book(models.Model): title = models ...
随机推荐
- 现在有一个函数A和函数B,请你实现B继承A
现在有一个函数A和函数B,请你实现B继承A // 方式1 function B(){} function A(){} B.prototype = new A(); // 方式2 function A( ...
- 题解 CF896C 【Willem, Chtholly and Seniorious】
貌似珂朵莉树是目前为止(我学过的)唯一一个可以维护区间x次方和查询的高效数据结构. 但是这玩意有个很大的毛病,就是它的高效建立在数据随机的前提下. 在数据随机的时候assign操作比较多,所以它的复杂 ...
- cacti1.1安装报错
安装过程中出现以下报错: ERROR: Your MySQL TimeZone database is not populated. Please populate this database bef ...
- 洛谷 P1583 魔法照片
P1583 魔法照片 题目描述 一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初 ...
- traits的介绍
traits基本利用了上一篇文章的思想,通过局部特化,来返回针对特定类型的信息. 可以自己实现traits,约定俗成了,返回特性信息. 也可以利用iterator的traits信息. iterator ...
- Android导航Tab栏实现
前言 android中滑动控件非常多,相信大部分同学接触的都是ListView这样的竖向滑动的控件,可是有时候我们也有横向滑动的需求,非常多应用眼下也做成了这个样子,以weiciyuan为例,例如以下 ...
- apiCloud如何打开新页面的同时,关掉当前页
方法很多,只要不同时open.close都可行.给你一个简单的方式: api.addEventListener({ name:'viewdisappear' },function(){ api.clo ...
- html2canvas截取页面
1.下载html2canvas.js 2.引入 3.修改html2canvas支持远程图片处理 function ImageContainer(src, cors) { this.src = src; ...
- 【理论基础】ContentProvider的简要概述
对于Android应用而言,他们必须相互独立,各自运行在自己的Dalvik虚拟机实例中,如果这些Android应用之间需要实现实时的数据交换——例如我们开发了一个发送短信的程序,当发送短信时需要从联系 ...
- 机器学习(四) 分类算法--K近邻算法 KNN (上)
一.K近邻算法基础 KNN------- K近邻算法--------K-Nearest Neighbors 思想极度简单 应用数学知识少 (近乎为零) 效果好(缺点?) 可以解释机器学习算法使用过程中 ...