POJ3625Building Roads

Building Roads

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 (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 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: Xand Y
* 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

 思路:简单的prim算法,这里与Prim算法不同的是,考虑到已经有默认边加入集合。 

#include <iostream>
#include<cstring>
#include<algorithm>
#include<stdio.h>
#include<cmath>
using namespace std;
double lowcost[1001],dist[1001][1001];
const double inf = 0x3f3f3f3f;
int vis[1010];//vis判断点是否加入到集合中
int n;
double prim(){
    //先把第一个点加入
    vis[1] = 1;
    int index;
    //初始化:第一个点到其他点的距离
    for(int i = 1; i <= n; ++i)
        lowcost[i] = dist[1][i];
    lowcost[1] = 0;

    double sum = 0;
    for(int i = 2; i <= n; ++i){//第一个for循环,目的是将所有点加入集合中,由于第一个点已经加入,所以从2开始
        double miner = inf;
        for(int j = 1; j <= n ; ++j){//第二个for循环,目的是找到最小的一条边
            if(!vis[j] && lowcost[j] < miner){
                miner = lowcost[j];//使用miner记录最小的距离
                index = j;//使用index记录最小边标号
            }
        }
        vis[index] = 1;
        sum += miner;// sum += lowcost[index];
        for(int j = 1; j <= n ; ++j){
            //更新最短距
            if(!vis[j] && lowcost[j] > dist[index][j]){
                lowcost[j] =dist[index][j];
            }
        }
    }
    return sum;
}
int main(){
    int m,A,B;
    double x[1001],y[1001];
    while(cin >> n >> m){
        for(int i = 1; i <= n; ++i){
            cin >> x[i] >> y[i];
        }
        memset(dist,inf,sizeof(dist));
        memset(lowcost,inf,sizeof(lowcost));
        memset(vis,0,sizeof(vis));
        for(int i = 1; i <= n; ++i){
            for(int j = i + 1; j <= n ; ++j){
               dist[i][j] = dist[j][i] = sqrt((x[j] - x[i])*(x[j] - x[i]) + (y[j] - y[i])* (y[j] - y[i]));
            }
        }
        for(int i = 1; i <= m ; ++i){
            cin >> A >> B;
            dist[A][B] = dist[B][A] = 0;
        }
        for(int i = 1; i <= n; ++i)
            dist[i][i] = 0;
        printf("%.2f
",prim());
    }
}
View Code

因上求缘,果上努力~~~~ 作者:每天卷学习,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/14307055.html

原文地址:https://www.cnblogs.com/BlairGrowing/p/14307055.html