Building Roads(Prim+Kruskal)

Building Roads
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11767 Accepted: 3351
Description

Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.

Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.

Input

  • Line 1: Two space-separated integers: N and M
  • Lines 2..N+1: Two space-separated integers: Xi and Yi
  • Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.

Output

  • Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.

Sample Input

4 1
1 1
3 1
2 3
4 3
1 4
Sample Output

4.00
Source

USACO 2007 December Silver

n个村庄,m条已经修好的道路,接下来n行表示n个村庄的坐标(xi,yi),m行表示两个村庄(a,b)已经修好道路。求n个村庄互相连通需要修建的最短道路。

Prim:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define INF 0x3f3f3f3f
double ma[1211][1211];
double dist[1211];
int v[1211];
int n, m;
int x[1211], y[1211];
double dis(int i, int j)
{
    double xx = x[i] - x[j];
    double yy = y[i] - y[j];
    return sqrt(xx*xx+yy*yy);
}
void Prim()
{
    memset(v, 0, sizeof(v));
    for(int i=0;i<n;i++)
        dist[i] = ma[0][i];
    v[0] = 1;
    int point;
    double ans = 0, min;
    for(int i=1;i<n;i++)
    {
        point = i;
        min = INF;
        for(int j=0;j<n;j++)
        {
            if(dist[j]<min&&v[j]==0)
            {
                point = j;
                min = dist[j];
            }
        }
        ans += min;
        v[point] = 1;
        for(int j=0;j<n;j++)
        {
            if(dist[j]>ma[point][j]&&v[j]==0)
                dist[j] = ma[point][j];
        }
    }
    printf("%.2f\n", ans);
}
int main()
{
    scanf("%d %d", &n, &m);
    for(int i=0;i<n;i++)
    {
        scanf("%d %d", &x[i], &y[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<i;j++)
        {
            ma[i][j] = ma[j][i] = dis(i, j);
        }
    }
    for(int i=0;i<m;i++)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        ma[a-1][b-1] = ma[b-1][a-1] = 0;//已经修好的清零
    }
    Prim();
    return 0;
}

Kruskal:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;
double x[121001];
double y[121001];
struct node
{
    int x, y;
    double data;
}que[1000000];
int n, m, top, cont;
int pre[121001];
bool cmp(struct node u, struct node b)
{
    return u.data<b.data;
}
double dis(int i, int j)
{
    double xx = x[i] - x[j];
    double yy = y[i] - y[j];
    return sqrt(xx*xx+yy*yy);
}
int root(int a)
{
    while(a!=pre[a])
        a = pre[a];
    return a;
}
void Kruskal()
{
    double ans = 0;
    for(int i=0;i<=top&&cont!=n-1;i++)
    {
        int xx = root(que[i].x);
        int yy = root(que[i].y);
        if(xx!=yy)
            {
                pre[xx] = yy;
                ans += que[i].data;
                cont++;//记录边数
            }
    }
    printf("%.2f\n", ans);
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
     cin>>x[i]>>y[i];
    top = -1, cont = 0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            top++;
            que[top].x = i;
            que[top].y = j;
            que[top].data = dis(i, j);
        }
    }
    for(int i=1;i<=n;i++)
        pre[i] = i;
    for(int i=0;i<m;i++)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        int xx = root(a);
        int yy = root(b);
        if(xx!=yy)
            {
                pre[xx] = yy;
                cont++;
            }
    }
    sort(que, que+top+1, cmp);
    Kruskal();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值