题干:

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 i th 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

题目大意:

首行给出N,代表有1~N共N个点。接下来N行,每行两个数x,y,代表第i个点的坐标。接着给出M,接着M行,每行两个数x,y,代表第x个点和第y个点已经联通(即x到y的权值为0),建立最小生成树,输出生成树中权值不为0的边的两端的点的编号。

思路:最坏的情况需要遍历图中的每一条边,很明显的稠密图,优先选用普利姆算法。 
建图:根据坐标建立无向图,权值设为距离的平方即可,这样可以避免sqrt后权值变为double型,避免精度损失。对于已联通的两点,更新这两点的权值为0即可。 
输出边:技巧看代码。

AC代码:

prim算法:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
//普利姆,注意建图技巧
const int maxn=751;
const int INF=0x3f3f3f3f;
int map[maxn][maxn];
int dis[maxn];
int vis[maxn];
int Edge[maxn];//i到Edge[i]是一条生成树内的边
struct node
{int x;int y;
} Point[maxn]; //第i个点的坐标
int N;//点的数量
int M;//更新边的数量
void init()
{scanf("%d",&N);for(int i=1; i<=N; i++)//建图{scanf("%d%d",&Point[i].x,&Point[i].y);for(int j=1; j<i; j++)//为什么这里不取sqrt,因为完全没必要map[i][j]=map[j][i]=(Point[i].x-Point[j].x)*(Point[i].x-Point[j].x)+(Point[i].y-Point[j].y)*(Point[i].y-Point[j].y);map[i][i]=INF;//自己不可能到自己}scanf("%d",&M);int x,y;while(M--)//更新图{scanf("%d%d",&x,&y);map[x][y]=map[y][x]=0;}memset(vis,0,sizeof(vis));vis[1]=1;for(int i=1; i<=N; i++){dis[i]=map[i][1];Edge[i]=1;//初始化为存储i到1的边}
}
void Prim()
{for(int i=1; i<N; i++){int minn=INF;int point_minn;for(int j=1; j<=N; j++)if(vis[j]==0&&minn>dis[j]){minn=dis[j];point_minn=j;}vis[point_minn]=1;for(int k=1; k<=N; k++)if(vis[k]==0&&dis[k]>map[point_minn][k]){Edge[k]=point_minn;//这里是输出方式的技巧dis[k]=map[point_minn][k];}if(map[Edge[point_minn]][point_minn])printf("%d %d\n",Edge[point_minn],point_minn);}
}
int main()
{init();Prim();return 0;
}
/*
题意:首行给出N,代表有1~N共N个点。接下来N行,每行两个数x,y,代表第i个点的坐标。接着给出M,接着M行,每行两个数x,y,代表第x个点和第y个点已经联通(即x到y的权值为0),建立最小生成树,输出生成树中权值不为0的边的两端的点的编号。思路:最坏的情况需要遍历图中的每一条边,很明显的稠密图,优先选用普利姆算法。
建图:根据坐标建立无向图,权值设为距离的平方即可,这样可以避免sqrt后权值变为double型,避免精度损失。对于已联通的两点,更新这两点的权值为0即可。
输出边:技巧看代码。
*/ 

克鲁斯卡尔:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int f[10000 + 5];struct Node {double x,y;
} node[10000 + 5];struct Node2 {int s,e;double c;
} node2[1000000 + 5] ;
int n,m;
int cnt;int getf(int v) {return v==f[v]?v:f[v]=getf(f[v]);
}
void merge(int u,int v) {int t1=getf(u);int t2=getf(v);if(t1!=t2) {f[t2]=t1;}
}
bool cmp(const Node2 & a,const Node2 & b) {return a.c<b.c;
}
double dis(const Node & a,const Node & b)
{return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main()
{int u,v;cin>>n;for(int i = 1; i<=n; i++) f[i]=i;//初始化    for(int i = 1; i<=n; i++) {scanf("%lf %lf",&node[i].x,&node[i].y);}cin>>m;while(m--) {scanf("%d %d",&u,&v);if(getf(u) != getf(v)) {           cnt++;//这是不对滴  比如 1 2   2 3   }merge(u,v);}int top=0;//top从1开始一直到n相当于模拟 for(int top = 1; top<=n; top++) 的过程只不过最后top==n for(int i = 1; i<=n; i++) {//那道 需要用i的值 然后排序 的题在哪来着? for(int j = i + 1; j<=n; j++) {if(getf(i) == getf(j) ) continue;//写getf(v) == getf(u)写顺手了? ++top;node2[top].s = i;node2[top].e = j;node2[top].c = dis(node[i], node[j]);//fabs(node[i].x-node[j].x)+fabs(node[i].y-node[j].y);//abs(node[u].x-node[v].x)+abs(node[u].y-node[v].y);}}sort(node2+1,node2+top+1,cmp);
//  printf("top = %d\n",top);
//  for(int i = 1; i<=top; i++) {
//      printf("%d  %d   %f\n",node2[i].s,node2[i].e,node2[i].c);
//  }for(int i = 1; i<=top; i++) {if(getf(node2[i].s) == getf(node2[i].e ) )continue;//这一步别忘!只有他俩尚未连通才能有下面的操作 merge(node2[i].s,node2[i].e); printf("%d %d\n",node2[i].s,node2[i].e);cnt++; if(cnt== n-1) break;} return  0 ;
}

【POJ - 1751】Highways (最小生成树)相关推荐

  1. poj 1751 Highways 最小生成树Kruskal(、Prim还没写

    OJ交不了,,, #pragma warning(disable:4996) #include<iostream> #include<string> #include<c ...

  2. POJ 1751 Highways (kruskal)

    题目链接:http://poj.org/problem?id=1751 题意是给你n个点的坐标,然后给你m对点是已经相连的,问你还需要连接哪几对点,使这个图为最小生成树. 这里用kruskal不会超时 ...

  3. POJ 2485 - Highways(求最小生成树的最大权值-Kruskal算法)

    题目 Language:Default Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36414 Accept ...

  4. POJ 2485 Highways(最小生成树 Prim)

    Highways   大意:给你一个用邻接矩阵形式存储的有n个顶点的无向图,让你求它的最小生成树并求出在这个生成树里面最大的边的权值. 思路:用Prim求,判断条件改一下就行. PS:dis数组初始化 ...

  5. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  6. * poj 1251 JungleRoad 最小生成树 Kruskal算法、Prim算法

    文章目录 Kruskal算法 模板:https://blog.csdn.net/Rain722/article/details/65642992 Prim算法 模板: poj 1251 JungleR ...

  7. POJ - 3026(BFS+最小生成树.krustal)

    题目: 题目链接: http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. poj 2485 Highways

    答案就是最小生成树中权值最大的边 #include<cstdio> #include<cstring> #include<cmath> #include<al ...

  9. POJ 3723 Conscription 最小生成树

    想清楚后能发现就要让我们求最小生成树(如果图连通的话,否则就是森林) //#pragma comment(linker, "/STACK:1024000000,1024000000" ...

最新文章

  1. 《评人工智能如何走向新阶段》后记(再续15)
  2. Linux期末复习题库(1)
  3. linux c warning: implicit declaration of function 'strtok_r'解决方案
  4. Mysql基础运维及复制架构——PRIT非完整恢复
  5. grep、cut、awk、sed文本处理
  6. 青少年蓝桥杯_2020_steam考试_初级组_第四题
  7. php输出字符unicode码,[PHP]单字符Unicode编码解码函数
  8. C语言基础语言总结(二)
  9. java类中,成员变量赋值第一个进行,其次是静态构造函数,再次是构造函数
  10. vue结合ueditor富文本编辑器(换肤分离)
  11. sqlite3_finalize sqlite3_close
  12. java简单创建图片面板_图像界面编程简单窗体创建
  13. as3 php,[AS3]as3.0与PHP程序通信源代码示例
  14. mysql特殊语句_mysql特殊语句 - BENNEE的个人空间 - OSCHINA - 中文开源技术交流社区...
  15. [Head First设计模式]生活中学设计模式——组合模式
  16. A*算法及其matlab实现
  17. 第三周 3.13-3.19
  18. 微信开发者工具安装使用SVN
  19. matlab中饱和函数怎么写,matlab中饱和函数如何表示呢
  20. win10环境下python3如何使用PyV8

热门文章

  1. [剑指offer]面试题第[57-2]题[JAVA][和为s的连续正数序列][数学法][滑动窗口]
  2. [Leedcode][JAVA][第85题][第221题][最大正方形][动态规划]
  3. java se程序设计课后答案,JAVA SE程序设计及实践
  4. python解压加密zip文件_Python:解压缩前检测一个zip文件否为加密,两种算法。
  5. docker 镜像修改的配置文件自动还原_PVE部署LXC运行docker
  6. linux桌面旋转了180度,[多图]回顾每一款默认Ubuntu壁纸
  7. 给与用户建立dblink的权限_网络安全 之 NTFS安全权限
  8. RxJava之PublishSubject、BehaviorSubject、ReplaySubject和AsyncSubject
  9. 网站显示不正常服务器怎么弄,你真的知道网站出现收录不正常的原因是什么吗...
  10. pc模式 华为mate30_号称“重构想象”的华为Mate30系列,到底有多颠覆?一睹为快...