ZOJ 2048 Highways 最小生成树 Kruskal && Prim
来源:互联网 发布:用爸妈身份证开淘宝店 编辑:程序博客网 时间:2024/05/20 09:46
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 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 should be created but it should be empty.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
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个城市,给定了已经连接起来的道路,输出的是城镇的编号,
最小生成树 Kruskal && Prim (注意格式要求 a blank line between output blocks)
此题 要输出路径,用Kruskal 较简单,用Prim 需要 标记
已AC代码:(Kruskal)
#include<cstdio>#include<cmath>#define MAX 1000000#include<algorithm>using namespace std;int n,m,per[1100]; // 并查集 int x[1100],y[1100];struct node{int u,v;double w; //w为距离 }s[MAX];bool cmp(node a,node b){return a.w<b.w;}double dlen(int i,int j){return sqrt( 1.0*( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) ) );}void into() //初始化 {for(int i=0;i<=n;++i)per[i]=i;}int find(int x) // 查找根节点 {return x==per[x]?x:per[x]=find(per[x]);}bool join(int a,int b) //合并根节点,并判断是否成环 {int fa=find(a);int fb=find(b);if(fa!=fb){per[fa]=fb;return true;}return false;}int main() {int T,i,j;scanf("%d",&T);while(T--){scanf("%d",&n);for(i=1;i<=n;++i)scanf("%d%d",&x[i],&y[i]);int k=0;for(i=1;i<n;++i){for(j=i+1;j<=n;++j){s[k].u=i;s[k].v=j;s[k].w=dlen(i,j);k++;}}into(); //初始化根节点 int a,b,c;scanf("%d",&m);while(m--){scanf("%d%d",&a,&b);c=join(a,b); //已连得路 }sort(s,s+k,cmp); //按距离从小到大排序 for(i=0;i<k;++i)if(join(s[i].u,s[i].v)) //输出的是城镇的编号,printf("%d %d\n",s[i].u,s[i].v);if(T!=0)printf("\n");}return 0;}
已AC代码:(Prim)
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define INF 0xfffffffusing namespace std;double map[1100][1100],low[1100];int n,m,vis[1100]; //map二维数组存图,low记录每2个点间最小权值,vis标记某点是否已访问int x[1100],y[1100];double dlen(int i,int j){return sqrt( 1.0*( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) ) );}double prim(){memset(vis,0,sizeof(vis));int i,j,pos,t,bj[1100]; //bj 标记已连的路 double MIN;for(i=1;i<=n;++i) //从某点开始,分别标记vis和记录该点pos{low[i]=map[1][i]; //第一次给low数组赋值 map的第一行 bj[i]=1;}vis[1]=1;pos=1;t=0;for(i=1;i<n;++i) //再运行n-1次,一次找一个最小 {MIN=INF;pos=0;for(j=1;j<=n;++j){if(vis[j]==0&&low[j]<MIN){MIN=low[j]; // 找出最小值min,记录位置pospos=j;}}if(map[bj[pos]][pos]!=0)printf("%d %d\n",pos,bj[pos]);vis[pos]=1; //标记该点已访问 for(j=1;j<=n;++j) //更新权值low 把 map的 pos 行中比对应的 low 小的赋给low if(vis[j]==0&&low[j]>map[pos][j]) {low[j]=map[pos][j];bj[j]=pos;}}}int main() {int T,i,j;scanf("%d",&T);while(T--){scanf("%d",&n);for(i=1;i<=n;++i)scanf("%d%d",&x[i],&y[i]);for(i=1;i<=n;++i)for(j=1;j<=n;++j)map[i][j]=INF;for(i=1;i<n;++i){for(j=i+1;j<=n;++j){map[i][j]=map[j][i]=dlen(i,j);}}int a,b,c;scanf("%d",&m);while(m--){scanf("%d%d",&a,&b); //已连的赋为 0 map[a][b]=map[b][a]=0;}prim();if(T!=0)printf("\n");}return 0;}
- ZOJ 2048 Highways 最小生成树 Kruskal && Prim
- POJ 1751 Highways 最小生成树 Kruskal && Prim
- POJ 2485 Highways 最小生成树 Kruskal && Prim
- zoj 2048 Highways(最小生成树)
- zoj 1406 最小生成树 kruskal 和 prim用法
- zoj 1203 Swordfish 【最小生成树 kruskal && prim】
- zoj 1586 QS Network【最小生成树 kruskal && prim】
- zoj 2326 Tangled in Cables【最小生成树 kruskal && prim】
- POJ2485 Highways(prim和Kruskal最小生成树两种解法)
- POJ 2485-Highways(最小生成树裸题-prim/kruskal)
- Prim、Kruskal最小生成树
- 最小生成树---Prim---Kruskal
- 最小生成树 Kruskal&&Prim
- 最小生成树 Prim Kruskal
- Kruskal/prim--最小生成树
- 最小生成树 PRIM KRUSKAL
- Sicily 1090 Highways(Kruskal最小生成树)
- poj 2485Highways(最小生成树 Kruskal)
- 传说中的快排是怎样的
- BMP文件格式详解
- 斯坦福ML公开课笔记11——贝叶斯正则化、在线学习、ML应用建议
- IOS上 关于状态栏的相关设置(UIStatusBar)
- hdu5378
- ZOJ 2048 Highways 最小生成树 Kruskal && Prim
- MFC程序如何去掉上面的工具栏
- SpringMVC之国际化
- 黑马程序员--java技术blog---第十篇:正则表达式
- Android 编程下 Touch 事件的分发和消费机制
- ognl表达式
- poj 3750 小孩报数问题(线段树-单点更新)
- google test
- hdu oj 1527 取石子游戏