F. Highways

Time Limit: 1000ms
Memory Limit: 10000KB

64-bit integer IO format: %lld      Java class name: Main

 
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.

 

Input

The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.

The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.

The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.

 

Output

Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.

If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.

 

Sample Input

9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2

Sample Output

1 6
3 7
4 9
5 7
8 3 解题:找出最小生成树的父节点。。。。。。。。。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
int mp[maxn][maxn];
struct pos {
int x,y;
} p[maxn];
int n,m,d[maxn],pre[maxn];
int dis(const pos &a,const pos &b) {
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
void prim(int src) {
int i,j,index,theMin;
for(i = ; i <= n; i++) {
pre[i] = src;
d[i] = mp[src][i];
}
pre[src] = -;
for(i = ; i < n; i++) {
theMin = INF;
index = -;
for(j = ; j <= n; j++) {
if(pre[j] != - && d[j] < theMin) theMin = d[index = j];
}
if(theMin != ) printf("%d %d\n",index,pre[index]);
pre[index] = -;
for(j = ; j <= n; j++) {
if(pre[j] != - && d[j] > mp[index][j]) {
d[j] = mp[index][j];
pre[j] = index;
}
}
}
}
int main() {
int i,j,u,v;
scanf("%d",&n);
for(i = ; i <= n; i++) scanf("%d %d",&p[i].x,&p[i].y);
for(i = ; i <= n; i++) {
for(j = i+; j <= n; j++)
mp[i][j] = mp[j][i] = dis(p[i],p[j]);
}
scanf("%d",&m);
while(m--) {
scanf("%d%d",&u,&v);
mp[u][v] = mp[v][u] = ;
}
prim();
return ;
}

图论trainning-part-1 F. Highways的更多相关文章

  1. 图论介绍(Graph Theory)

    1 图论概述 1.1 发展历史 第一阶段: 1736:欧拉发表首篇关于图论的文章,研究了哥尼斯堡七桥问题,被称为图论之父 1750:提出了拓扑学的第一个定理,多面体欧拉公式:V-E+F=2 第二阶段( ...

  2. Mysql_以案例为基准之查询

    查询数据操作

  3. [ACM_图论] Highways (变形说法的最小生成树)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28972#problem/C 题目给出T种情况,每种情况有n个城镇,接下来每一行是第i个城 ...

  4. 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs

    题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...

  5. 【图论好题】ABC #142 Task F Pure

    题目大意 给定一个 $N$ 个点 $M$ 条边的有向图 $G$,无重边.自环.找出图 $G$ 的一个导出子图(induced subgraph) $G'$,且 $G'$ 中的每个点的入度和出度都是 1 ...

  6. SPOJ HIGH Highways

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  7. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  8. F, A, MS, QM, RF的OFFER和经历 -- Final update

    昨天收到FB的电话,我的OFFER已经批下来了,这也意味着我的JOB HUNTING结束了,下 面是我这两个月来申请结果汇总: Applications (7): Facebook, Google, ...

  9. CJOJ 1070 【Uva】嵌套矩形(动态规划 图论)

    CJOJ 1070 [Uva]嵌套矩形(动态规划 图论) Description 有 n 个矩形,每个矩形可以用两个整数 a, b 描述,表示它的长和宽.矩形 X(a, b) 可以嵌套在矩形 Y(c, ...

随机推荐

  1. VS2015 VB.Net利用QrCodeNet生成QR Code

    Step by step Create QR Code with QrCodeNet Step.1 新建項目 Step.2 下載QrCodeNet代碼,解壓\QrCodeNet\sourceCode\ ...

  2. 精通AngularJS(三)深入scope,继承结构,事件系统和生命周期

    深入探讨 Scope 作用域 每一个 $scope 都是类 Scope 的一个实例.类 Scope 拥有可以控制 scope 生命周期的方法,提供事件传播的能力,并支持模板渲染. 作用域的层次结构 让 ...

  3. 内存泄露,C++

    内存泄露,C++通常是指new出来的内存没有delete掉.在你的代码里边,new了一块内存,然后马上就delete,自然就没有内存泄露了(呃……其实我也不确定啦,因为你用的是delete,而不是de ...

  4. iOS Category实现原理 (补充)

    iOS Category实现原理 (补充) load 和 initialize load load方法会在程序启动就会调用,当装载类信息的时候就会调用. 调用顺序看一下源代码.在 objc-loadm ...

  5. restful十项规范

    1.协议 API与用户的通信都是通过HTTPS协议进行的 2.域名 应尽量将API部署在专有域名下:https://api.example.com 如果确定API很简单,不会有什么扩展,则可以放在主域 ...

  6. tomcat+nginx 横向扩展

    1.分别在电脑上部署两个tomcat tomcat1  tomcat2 2.不是nginx 并启动 输入 localhost 并进入nginx欢迎界面,证明部署成功 3.修改nginx 配置 ngin ...

  7. HDinsight 系列-使用证书登陆中国区Azure

    使用azure explorer 插件的时候,登陆默认是globle的azure网站,中国区的azure不能直接使用 可以使用auth文件认证 auth 文件生成 az cloud show -o j ...

  8. Ajax 发送OPTION请求

    从fetch说起,用fetch构造一个POST请求. fetch('http://127.0.0.1:8000/api/login', { method: "POST", head ...

  9. Python字符编码补充

    字符编码: Python字符编码贯穿Python学习的始终,现在应用的是Python2中字符编码的问题是很多的. 这次是要彻底解决Python字符编码的问题!!! 1 字符编码的发展过程: 1 .AS ...

  10. (5)JSTL的xml标签库

    Jstl的XML标签库 JSTL提供了操作xml文件的标签库,使用xml标签库可以省去使用Dom和SAX标签库的繁琐,能轻松的读取xml文件的内容. <%@ taglib uri="h ...