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. js filter用法比较

    讲解一个很实用的JS小语法 filter 就是从数组中找到适合条件的元素(比如说大于某一个元素的值) var arr=[1,23,5,78,34,55,13]; 如何才能找到大于23的所有元素呢, 1 ...

  2. game 竞赛图 缩环

    [问题背景] zhx 和他的妹子(们)做游戏. [问题描述] 考虑 N 个人玩一个游戏, 任意两个人之间进行一场游戏 (共 N*(N-)/ 场) , 且每场一定能分出胜负. 现在,你需要在其中找到三个 ...

  3. I/O————字节流

    InputStream字节输入流 OutputStream字节输出流 用于以字节的形式读取和写入数据 下面是使用 字节输入流读取文件字节输出流写入文件 文件可能不存在,所以使用try catch pu ...

  4. mongodb 上限集合

    上限集合是固定大小的循环集合按照插入以支持高性能的创建,读取和删除操作.通过循环,这意味着,当分配给该集合中的固定大小要用尽时,它会开始删除集合中最旧的文件而不提供任何明确的命令. 上限集合限制更新, ...

  5. 洛谷P4017 最大食物链计数

    拓扑排序板子题 #include <iostream> #include <cstdio> #include <cstring> #include <queu ...

  6. 学习php中的mysql()函数

    1.mysql_connect(1,2,3):连接数据库服务器语句,一般常用这三个参数(1)数据库服务器地址,(2)用户名,(3)密码:常与die()(或者exit())函数结合使用:die()函数用 ...

  7. THML5新增功能

    HTML5新增功能 1.语义化标记: 1)article:article标签装载显示一个独立的文章内容.例如一篇完整的论坛帖子,一则网站新闻,一篇博客文章等等,一个用户评论等等 artilce可以嵌套 ...

  8. 如何让局域网其他计算机访问您的Mysql???

    一.配置Mysql:(修改mysql数据库中user表) mysql -u root -p // root用户登录mysql>use mysql; // 选择mysql数据库 mysql> ...

  9. iphone之打开pdf、doc、xls文件用UIWebView

    //文件名字及类型 NSString *path=[[NSBundle mainBundle]pathForResource:@"xls1" ofType:@"xls&q ...

  10. 51nod 算法马拉松17 解题报告 以后不能赛中写题解(查逐梦者抄袭本人代码...

    B题(数学题: 问(1+sqrt(2)) ^n  能否分解成 sqrt(m) +sqrt(m-1)的形式  如果可以 输出 m%1e9+7 否则 输出no n<=1e18 刚看题没思路 暴力一下 ...