其他分享
首页 > 其他分享> > poj1751kruskal

poj1751kruskal

作者:互联网

Highways
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26772   Accepted: 7803   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 ithtown (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

Northeastern Europe 1999

Accepted

 

//G++ Accepted 
//C++TLE
#include<cstdio> #include<cmath> #include<math.h> #include<algorithm> using namespace std; typedef long long ll; const int maxn=760; int x[maxn],y[maxn]; int ans1[maxn*maxn],ans2[maxn*maxn]; int S[maxn];//并查集 struct Edge{ int v1,v2;//边的两个顶点 double w;//权重 } edge[maxn*maxn];//定义边 bool cmp(Edge a,Edge b) { return a.w<b.w;//按边的权重由小到大排序; } void init_set() { for(int i=1;i<=maxn;i++){ S[i]=i; } } int find_set(int x) {//查询并查集,返回u的根结点 if(x!=S[x]) S[x]=find_set(S[x]);//路径压缩 return S[x]; } int merge_set(int x,int y) {//合并 x=find_set(x); y=find_set(y); if(x!=y) S[x]=S[y]; else return 0; return 1; } int n,m;//点,边 void kruskal() { int q=0; for(int i=1;i<=m;i++){ if(merge_set(edge[i].v1,edge[i].v2)){ printf("%d %d\n",edge[i].v1,edge[i].v2); } } } int main() { m=0; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&x[i],&y[i]); } for(int i=1;i<n;i++){ for(int j=i+1;j<=n;j++){ double tem=sqrt((double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]))); m++; edge[m].v1=i,edge[m].v2=j,edge[m].w=tem; } } init_set();//并查集初始化 // sort(edge+1,edge+1+m,cmp);//把边按权重由小到大排序 int ex; scanf("%d",&ex); int t=ex; while(ex--){ int x,y; scanf("%d%d",&x,&y); merge_set(x,y); } sort(edge+1,edge+1+m,cmp);//把边按权重由小到大排序 kruskal(); return 0; }

 

以下是使用优化后的并查集,没有ac,苦恼,求解答

#include<cstdio>
#include<cmath>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=760;
int x[maxn],y[maxn];
int ans1[maxn*maxn],ans2[maxn*maxn];
int S[maxn],height[maxn];//并查集
struct Edge{
    int v1,v2;//边的两个顶点 
    double w;//权重 
} edge[maxn*maxn];//定义边
bool cmp(Edge a,Edge b)
{
    return a.w<b.w;//按边的权重由小到大排序; 
}
void init_set()
{
    for(int i=1;i<=maxn;i++){
        S[i]=i;
        height[i]=0;//树的高度//其实全局变量初始化就已经全为0; 
    }
} 
int find_set(int x)
{//查询并查集,返回u的根结点 
    int r=x;
    while(S[r]!=r) r=S[r];
    int i=x,j;
    while(i!=r){
        j=S[i];
        S[i]=r;
        i=j;
    }
    return r;
}
int merge_set(int v1,int v2)
{//合并 
    int a=find_set(v1);//找到根节点 
    int b=find_set(v2);
    int flag=1; 
    if(a==b) flag=0;//根节点相同,做标记返回 
    if(height[a]==height[b]){
        height[a]+=1;
        S[b]=a;
    }
    else{//把矮树并到高数上,高树的高度保持不变
        if(height[a]<height[b]) S[a]=b;
        else S[b]=a;
    }
    return flag;
}
int n,m;//点,边
void kruskal()
{
    int q=0;
    for(int i=1;i<=m;i++){
        if(edge[i].w)
        if(merge_set(edge[i].v1,edge[i].v2)){
            printf("%d %d\n",edge[i].v1,edge[i].v2);
        }
    } 
} 
int main()
{
    m=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&x[i],&y[i]);
    }
    for(int i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            double tem=sqrt((double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
            m++;
            edge[m].v1=i,edge[m].v2=j,edge[m].w=tem;
        }
    }
    init_set();//并查集初始化
//    sort(edge+1,edge+1+m,cmp);//把边按权重由小到大排序 
    int ex;
    scanf("%d",&ex);
    int t=ex;
    while(ex--){
        int x,y;
        scanf("%d%d",&x,&y);
        if(x>y){
            int t=x;
            x=y;
            y=t;
        }
        int po;
        if(x==1){
            po=y;
        }
        else if(x==2) po=n-1+y;
        else po=(n-1+n-x+1)*(x-1)/2+y;
        edge[po].w=0;
        merge_set(x,y);
    }
    sort(edge+1,edge+1+m,cmp);//把边按权重由小到大排序 
    kruskal();
    return 0;
} 

 

 

标签:int,poj1751kruskal,highways,maxn,towns,include,highway
来源: https://www.cnblogs.com/littleyyhome/p/11924181.html