链接:

http://poj.org/problem?id=1751

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11507   Accepted: 3279   Special Judge

Description

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<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 800
#define INF 0xffffff struct node {int x, y;}a[N]; int n, m, pre[N];
bool used[N][N], vis[N];
double G[N][N], dist[N]; void prim()
{
int i, j;
for(i=; i<=n; i++)
{
dist[i] = G[][i];
pre[i] = ;
} vis[] = ; for(i=; i<n; i++)
{
int Index = ;
double Min = INF;
for(j=; j<=n; j++)
{
if(!vis[j] && Min>dist[j])
{
Min = dist[j];
Index = j;
}
} vis[Index] = ; for(j=; j<=n; j++)
{
if(!vis[j] && dist[j] > G[Index][j])
{
dist[j] = G[Index][j];
pre[j] = Index;
}
}
}
} int main()
{
while(scanf("%d", &n)!=EOF)
{
int i, j, u, v; for(i=; i<=n; i++)
scanf("%d%d", &a[i].x, &a[i].y); for(i=; i<=n; i++)
for(j=; j<=i; j++)
G[i][j] = G[j][i] = sqrt(1.0*(a[i].x-a[j].x)*(a[i].x-a[j].x) + (a[i].y-a[j].y)*(a[i].y-a[j].y)); scanf("%d", &m);
memset(used, , sizeof(used));
for(i=; i<=m; i++)
{
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = ;
used[u][v] = used[v][u] = ;
} memset(vis, , sizeof(vis));
prim(); for(i=; i<=n; i++)
{
if(!used[pre[i]][i] && !used[i][pre[i]])
printf("%d %d\n", i, pre[i]);
}
}
return ;
}

(最小生成树 Prim) Highways --POJ --1751的更多相关文章

  1. H - Highways - poj 1751(prim)

    某个地方政府想修建一些高速公路使他们每个乡镇都可以相同通达,不过以前已经修建过一些公路,现在要实现所有的联通,所花费的最小代价是多少?(也就是最小的修建长度),输出的是需要修的路,不过如果不需要修建就 ...

  2. Highways POJ - 1751

    题目链接:https://vjudge.net/problem/POJ-1751 思路: 最小生成树板子,只需要多记录每个dis[x]的权值是从哪个点到x这个点的. #include <stdi ...

  3. POJ 1751 Highways (最小生成树)

    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  4. Highways POJ-1751 最小生成树 Prim算法

    Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输 ...

  5. 数据结构代码整理(线性表,栈,队列,串,二叉树,图的建立和遍历stl,最小生成树prim算法)。。持续更新中。。。

    //归并排序递归方法实现 #include <iostream> #include <cstdio> using namespace std; #define maxn 100 ...

  6. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  7. 最小生成树Prim算法(邻接矩阵和邻接表)

    最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...

  8. 转载:最小生成树-Prim算法和Kruskal算法

    本文摘自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html 最小生成树-Prim算法和Kruskal算法 Prim算 ...

  9. 最小生成树Prim

    首先解释什么是最小生成树,最小生成树是指在一张图中找出一棵树,任意两点的距离已经是最短的了. 算法要点: 1.用book数组存放访问过的节点. 2.用dis数组保存对应下标的点到树的最近距离,这里要注 ...

随机推荐

  1. Delphi声明Record变量后直接初始化

    TARec = record    A1: string;    A2: string;  end; TBRec = record    A1: string;    A2: string;    A ...

  2. iBatis与Hibernate有什么不同?

    相同点:屏蔽jdbc api的底层访问细节,使用我们不用与jdbc api打交道,就可以访问数据. jdbc api编程流程固定,还将sql语句与java代码混杂在了一起,经常需要拼凑sql语句,细节 ...

  3. HTML CSS + DIV实现整体布局 part2

    9.盒模型的层次关系 我们通过一个经典的盒模型3D立体结构图来理解,如图:     从上往下看,层次关系如下: 第1层:盒子的边框(border),     第2层:元素的内容(content).内边 ...

  4. oracle系统视图字段说明

    oracle系统表v$session.v$sql表的列字段说明‍ 在本视图中,每一个连接到数据库实例中的 session都拥有一条记录.包括用户 session及后台进程如 DBWR, LGWR, a ...

  5. mybatis动态sql trim

    trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: 1. select * from user <trim prefix="WHERE" p ...

  6. windows服务启动的进程无窗口

    勾选允许服务与桌面交互 指服务是否在桌面上提供用户界面,当服务启动后不论是谁登录都能使用.只有作为 LocalSystem 帐户(由“此帐户”指定)运行时,该选项才能使用. 如果一个服务需要界面(比如 ...

  7. git仓库搬家

    1). 从原地址克隆一份裸版本库 git clone --bare git://xxxxx.com/xxx.git 2). 然后到新的 Git 服务器上创建一个新项目 3). 以镜像推送的方式上传代码 ...

  8. XX-net 部署网络

    https://github.com/XX-net/XX-Net/wiki/%E4%BD%BF%E7%94%A8Chrome%E6%B5%8F%E8%A7%88%E5%99%A8 主要步骤有三步:安装 ...

  9. Java中的Filter

    filter过滤器主要使用于前台向后台传递数据是的过滤操作.程度很简单就不说明了,直接给几个已经写好的代码: 一.使浏览器不缓存页面的过滤器 import javax.servlet.*; impor ...

  10. sqlite小知识

    删除数据时,由于缓存关系,数据了文件大小不会一下子减小,可以通过执行vacuum;或新建表时使用自动整理大小来实现. sqlite的大小理论上可以达到140T. 暂时,使用C的api,只能使用不是.开 ...