Highways --(Kruskal和prim)C - 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


題意:在城市間修建高速公路,要求最短且全部兩兩連通,有幾個已經修建完畢,現在求還需要在兩城市之間修建的城市編號。
題解:我當時是用Kruskal寫的,結果總是不對,就是因爲我賦f[i]定義在輸出下面了,導致沒把已經修建的城市給圈起來,Kruskal還需要注意的就是把座標的城市編號賦值時,內循環一定要從i+1開始,不然會超時,這題推薦用Prime做。


Kruskal  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");
    }
}
Prim ac代碼:
#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);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章