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.
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.
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.
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.
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.
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.
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
1 6 3 7 4 9 5 7 8 3
题意:在城市间修建高速公路,要求最短且全部两两连通,有几个已经修建完毕,现在求还需要在两城市之间修建的城市编号。
题解:我当时是用Kruskal写的,结果总是不对,就是因为我赋f[i]定义在输出下面了,导致没把已经修建的城市给圈起来,Kruskal还需要注意的就是把坐标的城市编号赋值时,内循环一定要从i+1开始,不然会超时,这题推荐用Prime做。
Kruskal ac代码:Prim ac代码:#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; struct node { double u,v; //此处最好定义为double型 } e[1110000]; struct edge { int a,b; double s; //这里需要定义为double型,数值过大——int不够 } q[1110000]; double cmp(edge a,edge b) //开头千万不能写int,前面定义为double型 { return a.s<b.s; } int n,m,t1,t2; int f[2050],t=0,g; int sum; int getf(int v) { if(f[v]==v) return v; else { f[v]=getf(f[v]); return f[v]; } } int merg(int v,int u) { int t1,t2; t1=getf(v); t2=getf(u); if(t1!=t2) { f[t2]=t1; return 1; } return 0; } void prime() { int i,j; g=0; for(i=1; i<=n; i++) { for(j=i+1; j<=n; j++) //从i+1开始,减少复杂度 { q[g].a=i; //将坐标的城市号和两号之间距离存起来 q[g].b=j; q[g].s=(e[j].u-e[i].u)*(e[j].u-e[i].u)+(e[j].v-e[i].v)*(e[j].v-e[i].v); g++; } } } int main() { while(~scanf("%d",&n)) { int i,j; sum=0; t=0; for(i=1; i<=n; i++) f[i]=i; for(i=1; i<=n; i++) scanf("%lf%lf",&e[i].u,&e[i].v); //输入从1到n的坐标 scanf("%d",&m); for(i=0; i<m; i++) { scanf("%d%d",&t1,&t2); //已经建好的就让他们圈入一个整体 merg(t1,t2); } prime(); sort(q,q+g,cmp); for(i=0; i<g; i++) //Kruskal核心算法 { if(merg(q[i].a,q[i].b)) { t++; printf("%d %d\n",q[i].a,q[i].b); } if(t==n-1) break; } if(t==0) printf("\n"); } }
#include<stdio.h> #include<string.h> #define inf 0x3f3f3f3f int t,n,m,minn,t1,t2,t3; int dis[1000],book[1000],mp[1000][1000],xx[1000]; struct node { int u,v; }e[1000]; int init(int x,int y) { int m1=(e[x].u-e[y].u)*(e[x].u-e[y].u); int m2=(e[x].v-e[y].v)*(e[x].v-e[y].v); int m3=m1+m2; return m3; } void prim(int v) //Prim核心算法 { memset(xx,0,sizeof(xx)); int i,j,k; for(i=1;i<=n;i++) { dis[i]=mp[v][i]; xx[i]=v; } book[v]=1; t++; while(t<n) { minn=inf; j=v; for(i=1;i<=n;i++) { if(book[i]==0&&dis[i]<minn) { minn=dis[i]; j=i; } } book[j]=1; t++; if(minn!=0) printf("%d %d\n",j,xx[j]); for(k=1;k<=n;k++) { if(book[k]==0&&dis[k]>mp[j][k]) { dis[k]=mp[j][k]; xx[k]=j; } } } } int main() { while(~scanf("%d",&n)) { int i,j; memset(mp,0,sizeof(mp)); memset(dis,0,sizeof(dis)); for(i=1;i<=n;i++) scanf("%d%d",&e[i].u,&e[i].v); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(i==j) mp[i][j]=0; else mp[i][j]=init(i,j); //将两个坐标的距离赋给mp } scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d%d",&t1,&t2); mp[t1][t2]=0; mp[t2][t1]=0; } memset(book,0,sizeof(book)); prim(1); } }