天天看點

POJ:1751-Highways(Kruskal和Prim)

Highways

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 6078 Accepted: 1650 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 ith 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

解題心得:

  1. 題目很簡單,就是跑一個最小生成樹,然後記錄需要建立的新邊。但是很坑啊,題目中說如果沒有輸出那麼就會建立一個空白的檔案,是以如果是寫的多組輸入,就會WA,不知道為啥,可能是沒有建立空白的新檔案吧。
  2. 然後就是邪最小生成樹,兩種寫法
    • Kruskal算法先得出每一條邊,然後對邊排序,從小的邊開始選擇,用并查集來判斷是否形成了環。
    • Prim算法是選擇一個點,然後找出距離這個點最近的一個點,連成邊,然後以找出的店為目标,再從沒被連接配接的點中找出一個距離最近的點,連成邊,然後一直将所有的點全部連接配接。

Kruskal算法代碼:

#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = +;
struct node
{
    int x,y;
} p[maxn*maxn];
struct Path
{
    int s,e,len;
} path[maxn*maxn];
int n,m,father[maxn*maxn],ans,t;
vector <pair<int,int> > ve;

bool cmp(Path a,Path b)
{
    return a.len<b.len;
}

int find(int x)
{
    if(father[x] == x)
        return x;
    return father[x] = find(father[x]);
}

void merge(int x,int y)
{
    int fx = find(x);
    int fy = find(y);
    if(fx != fy)
        father[fy] = fx;
}

int dis(int x,int y)
{
    int d = (p[x].x - p[y].x)*(p[x].x - p[y].x) + (p[x].y - p[y].y)*(p[x].y - p[y].y);
    return d;
}

void init()
{
    ans = t = ;
    for(int i=; i<=n; i++)
    {
        father[i] = i;
        scanf("%d%d",&p[i].x,&p[i].y);
    }
    //枚舉每一條邊
    for(int i=; i<=n; i++)
        for(int j=i+; j<=n; j++)
        {
            path[t].s = i;
            path[t].e = j;
            path[t++].len = dis(i,j);
        }
    sort(path,path+t,cmp);//将邊從小到大排序
    cin>>m;
    for(int i=; i<m; i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(find(a) != find(b))
            merge(a,b);//将已經有路的點合并
    }
}

void solve()
{

    for(int i=; i<t; i++)
    {
        int x = path[i].s;
        int y = path[i].e;
        int len = path[i].len;
        if(find(x) != find(y))//如果不是同一個祖先那麼連接配接就不會形成環
        {
            ans += len;
            merge(x,y);
            ve.push_back(make_pair(x,y));//記錄需要連接配接的點
        }
    }
    for(int i=; i<ve.size(); i++)
    {
        pair<int,int> p;
        p = ve[i];
        printf("%d %d\n",p.first,p.second);
    }
    ve.clear();
}

int main()
{
    cin>>n;
    init();
    solve();
    return ;
}
           

Prim算法代碼

#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = ;
//lowcast記錄的是各點距離已經生成了的樹的距離
int maps[maxn][maxn],lowcost[maxn],n,m,Edge[maxn];
bool vis[maxn];//記錄點是否已經在樹中
struct NODE
{
    int x,y;
}node[maxn];

int get_dis(int x,int y)
{
    int dis = (node[x].x - node[y].x)*(node[x].x - node[y].x) + (node[x].y - node[y].y)*(node[x].y - node[y].y);
    return dis;
}

void init()
{
    cin>>n;
    for(int i=;i<=n;i++)
    {
        scanf("%d%d",&node[i].x,&node[i].y);
        for(int j=;j<i;j++)
            maps[i][j] = maps[j][i] = get_dis(i,j);//記錄兩點之間的距離
        maps[i][i] = ;//不可能自身到自身
    }

    memset(vis,,sizeof(vis));//記錄該點是否已經在樹上
    vis[] = ;
    cin>>m;
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        maps[a][b] = maps[b][a] = ;
    }
    for(int i=;i<=n;i++)
    {
        lowcost[i] = maps[i][];//先得到所有點距離第一個點的距離
        Edge[i] = ;
    }
}

void Prim()
{
    for(int i=;i<n;i++)
    {
        int Min = ;
        int point;
        for(int j=;j<=n;j++)//目前樹距離最近的點
            if(!vis[j] && Min > lowcost[j])
            {
                Min = lowcost[j];
                point = j;
            }
        vis[point] = true;//将這個點加入樹中
        for(int k=;k<=n;k++)
        {
            if(!vis[k] && lowcost[k] > maps[point][k])
            {
                Edge[k] = point;//記錄添加邊的兩個點 
                lowcost[k] = maps[point][k];
            }
        }
        if(maps[Edge[point]][point])
            printf("%d %d\n",Edge[point],point);
    }
}

int main()
{
    init();
    Prim();
    return ;
}