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. kafka基础三

    消费者消费消息过程(一) 消费者组 消费者是以消费者组consumer group的方式进行消息消费的,一个消费者组是由一个或者多个消费者组成的,共同消费一个topic,在每个分区同一时间只能由消费者 ...

  2. hdoj薛猫猫杯程序设计网络赛1003 球球大作战

    思路: 二分,check函数不是很好写. 实现: 1. #include <bits/stdc++.h> using namespace std; typedef long long ll ...

  3. Ubuntu安装新英伟达驱动出现问题解决方法

    ERROR: The Nouveau kernel driver is currently in use by your system. This driver is incompatible wit ...

  4. sql中的exsits和not exsits

    select * from table where exsits(sql语句) :  括号中sql语句有数据则返回这些相关id的数据集 select * from table where not ex ...

  5. MSSQL复制表

    -复制表结构有句型的--跨数据库 --复制结构+数据 select * into 数据库名.dbo.新表名 from 数据库名.dbo.原表名 --只复制结构 select * into 数据库名.d ...

  6. Kubenetes里pod和service绑定的实现方式

    我之前的文章 如何在Kubernetes里创建一个Nginx service介绍了如何创建一个Kubernetes pod和service,使用的方法是命令kubectl run. 本文介绍另一种方式 ...

  7. SQLite - WHERE子句

    SQLite - WHERE子句 SQLite WHERE子句用于指定一个条件同时抓取数据从一个表或多个表. 如果给定的条件满意,意味着true,然后从表中返回特定值.你会使用WHERE子句来筛选记录 ...

  8. make 与makefile(会不会写 makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。)

    跟我一起写 Makefile /**/ 陈皓 (CSDN) 概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉 ...

  9. WPF知识点全攻略05- XAML内容控件

    此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...

  10. rpn网络结构再分析

    这是rpn网络train阶段的网络结构图 rpn_conv1之前的网络是特征提取层,也是和fast rcnn共享的层.rpn_conv1是一层1*1的卷积,这一层是单独为rpn网络多提取一层特征,这一 ...