Highways POJ - 1751


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个城市的坐标 M个已建成的城市之间的路,求出需要修建路的城市编号。

       两种方法,prim 或是 kruskal,

     prim: 用两个for循环将i城市到j城市的长度d=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])(因为最终结果只需输出城市的编号,所以d就不用开方了)存在二维数组e[i][j]=e[j][i];(这个图属于无向图,注意存两个方向)  然后 输入 M 条已经修好的路须把它们的d 存为0(e[a][b]=e[b][a]=0),用一维数组 dis[]存1-n城市的最短路,p[]存需要修路的城市编号,如果两个城市的路存在 输出 编号即可,

#include<stdio.h>
#include<string.h>
#define df 99999999
int e[800][800],book[800],dis[800],p[800];
int n,m,x[800],y[800];
int main()
{
    while(~scanf("%d",&n))
    {
        int i,j,k,b,c,num=0,min;
        for(i=1; i<=n; i++)
            scanf("%d%d",&x[i],&y[i]);
        scanf("%d",&m);
        for(i=1; i<=n; i++)
        {
            for(j=i; j<=n; j++)
            {
                if(i==j)
                    continue;
                int d=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
                e[i][j]=d;
                e[j][i]=d;
            }
        }
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&b,&c);
            e[b][c]=0;
            e[c][b]=0;
        }
        for(i=1; i<=n; i++)
        {
            dis[i]=e[1][i];
            p[i]=1;
        }
        book[1]=1;
        num++;
        while(num<n)
        {
            min=df;
            for(i=1; i<=n; i++)
            {
                if(book[i]==0&&dis[i]<min)
                {
                    min=dis[i];
                    j=i;
                }
            }
            book[j]=1;
            num++;
            if(e[p[j]][j])
                printf("%d %d\n",j,p[j]);
            for(k=1; k<=n; k++)
            {
                if(book[k]==0&&dis[k]>e[j][k])
                {
                    dis[k]=e[j][k];
                    p[k]=j;
                }
            }
        }
    }
    return 0;
}

kruskal:与并查集结合,Find 找根节点,merge 合并两个元素,用结构体存城市编号和长度,用sort按长度从小到大排序   输入已经修过的城市编号 并合并 merge(a,b);表示不需要再修,最后用以for循环来判断是否修过 ,没修过的 输出编号即可。

#include<stdio.h>
int f[100001],book[100001];
int flag,num;
int Find(int x)
{
    if(f[x]==x)
        return x;
    else
        return f[x]=Find(f[x]);
}
void merge(int x,int y)
{
    int t1=Find(x);
    int t2=Find(y);
    if(t1!=t2)    //表示在一个集合中
        f[t2]=t1;
    else
        flag=0; //是否有环
}
int main()
{
    int i,x,y;
    while(~scanf("%d%d",&x,&y))
    {
        if(x==-1&&y==-1)
            break;
        if(x==0&&y==0)
        {
            printf("Yes\n");
            continue;
        }
        for(i=0; i<=100000; i++)//保证只有一个集合所以需要标记
        {
            f[i]=i;
            book[i]=0;
        }
        flag=1;
        merge(x,y);
        book[x]=1;
        book[y]=1;
        while(~scanf("%d%d",&x,&y)&&(x||y))
        {
            merge(x,y);
            book[x]=1;
            book[y]=1;
        }
        if(flag==0)
        {
            printf("No\n");
            continue;
        }
        else   
        {
            num=0;
            for(i=0; i<=100000; i++)
            {
                if(book[i]&&f[i]==i)//f[i]==i表示只有一个集合
                    num++;
            }
            if(num==1)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

prim:
#include<stdio.h>
#include<string.h>
#define df 99999999
int e[800][800],book[800],dis[800],p[800];
int n,m,x[800],y[800];
int main()
{
    while(~scanf("%d",&n))
    {
        int i,j,k,b,c,num=0,min;
        for(i=1; i<=n; i++)
            scanf("%d%d",&x[i],&y[i]);
        scanf("%d",&m);
        for(i=1; i<=n; i++)
        {
            for(j=i; j<=n; j++)
            {
                if(i==j)
                    continue;
                int d=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
                e[i][j]=d;
                e[j][i]=d;
            }
        }
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&b,&c);
            e[b][c]=0;
            e[c][b]=0;
        }
        for(i=1; i<=n; i++)
        {
            dis[i]=e[1][i];
            p[i]=1;
        }
        book[1]=1;
        num++;
        while(num<n)
        {
            min=df;
            for(i=1; i<=n; i++)
            {
                if(book[i]==0&&dis[i]<min)
                {
                    min=dis[i];
                    j=i;
                }
            }
            book[j]=1;
            num++;
            if(e[p[j]][j])
                printf("%d %d\n",j,p[j]);
            for(k=1; k<=n; k++)
            {
                if(book[k]==0&&dis[k]>e[j][k])
                {
                    dis[k]=e[j][k];
                    p[k]=j;
                }
            }
        }
    }
    return 0;
}

kruskal:
#include<stdio.h>
#include<algorithm>
using namespace std;
int f[1000],x[1000],y[1000],n,m,num=0;
struct note
{
    int u;
    int v;
    int w;
} e[800*800];
int Find(int x)
{
    if(f[x]==x)
        return x;
    else
        return Find(f[x]);
}
int cmp(note a,note b)
{
    return a.w<b.w;
}
int merge(int x,int y)
{
    int t1=Find(x);
    int t2=Find(y);
    if(t1 != t2)
    {
        f[t2] = t1;
        return 1;
    }
    return 0;
}
int main()
{
    while(~scanf("%d",&n))
    {
        int i,j,b,c,d,sum=0;
        for(i=1; i<=n; i++)
            f[i]=i;
        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++)
            {
                d=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
                e[k].u=i;
                e[k].v=j;
                e[k++].w=d;
            }
        }
        scanf("%d",&m);
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&b,&c);
            merge(b,c);
        }
        sort(e,e+k,cmp);
        for(i=0; i<k; i++)
        {
            if(merge(e[i].u,e[i].v))
            {
                printf("%d %d\n",e[i].u,e[i].v);
            }
        }
    }
    return 0;
}















  













































































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值