Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %lld & %llu |
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 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
Source
有n个点,其中一些点已经连通,求最小生成树。
因为是小范围稠密图,所以用邻接矩阵就可以。把已经连通的点之间的距离设为0,之后跑Prim,如果新选用的边长度不为0就输出。
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 const int mxn=1810; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 13 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 14 return x*f; 15 } 16 struct point{ 17 int x,y; 18 }p[mxn]; 19 double dist(int a,int b){ 20 return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y)); 21 } 22 double mp[mxn][mxn]; 23 int ans[mxn]; 24 int n,m; 25 int cm[mxn]; 26 double dis[mxn]; 27 bool vis[mxn]; 28 void Prim(){ 29 int i,j; 30 for(i=1;i<=n;i++){ 31 dis[i]=mp[1][i]; 32 cm[i]=1;//?????????? 33 } 34 dis[1]=0;vis[1]=1; 35 for(i=1;i<=n;i++){ 36 double mini=1e9;int pos=0; 37 for(j=1;j<=n;j++){ 38 if(!vis[j] && dis[j]<mini){ 39 mini=dis[j]; 40 pos=j; 41 } 42 } 43 if(!pos)break; 44 if(mp[cm[pos]][pos])printf("%d %d\n",cm[pos],pos); 45 vis[pos]=1; 46 for(j=1;j<=n;j++){ 47 if(!vis[j] && dis[j]>mp[pos][j]){ 48 dis[j]=mp[pos][j]; 49 cm[j]=pos; 50 } 51 } 52 } 53 return; 54 } 55 int main(){ 56 int i,j; 57 n=read(); 58 for(i=1;i<=n;i++){ 59 p[i].x=read();p[i].y=read(); 60 } 61 for(i=1;i<n;i++) 62 for(j=i+1;j<=n;j++) 63 mp[i][j]=mp[j][i]=dist(i,j); 64 m=read(); 65 int u,v; 66 for(i=1;i<=m;++i){ 67 u=read();v=read(); 68 mp[u][v]=mp[v][u]=0; 69 } 70 Prim(); 71 return 0; 72 }
/*by SilverN*/ #include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<vector> using namespacestd; const int mxn=1810; int read(){ int x=0,f=1;char ch=getchar(); while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } structpoint{ int x,y; }p[mxn]; double dist(int a,int b){ return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y)); } double mp[mxn][mxn]; int ans[mxn]; int n,m; int cm[mxn]; double dis[mxn]; bool vis[mxn]; void Prim(){ int i,j; for(i=1;i<=n;i++){ dis[i]=mp[1][i]; cm[i]=1;//?????????? } dis[1]=0;vis[1]=1; for(i=1;i<=n;i++){ double mini=1e9;int pos=0; for(j=1;j<=n;j++){ if(!vis[j] && dis[j]<mini){ mini=dis[j]; pos=j; } } if(!pos)break; if(mp[cm[pos]][pos])printf("%d %d\n",cm[pos],pos); vis[pos]=1; for(j=1;j<=n;j++){ if(!vis[j] && dis[j]>mp[pos][j]){ dis[j]=mp[pos][j]; cm[j]=pos; } } } return; } int main(){ int i,j; n=read(); for(i=1;i<=n;i++){ p[i].x=read();p[i].y=read(); } for(i=1;i<n;i++) for(j=i+1;j<=n;j++) mp[i][j]=mp[j][i]=dist(i,j); m=read(); int u,v; for(i=1;i<=m;++i){ u=read();v=read(); mp[u][v]=mp[v][u]=0; } Prim(); return 0; }