Building Roads 【POJ - 3625】【Prime最小生成树】

题目链接


  一道简单的MST(最小生成树),但是一开始可能不在状态,敲了个Kruskal,然后疯狂RE,后来再想想,为什么不把已经相连好的节点处理成点间距为0不就好了嘛。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define INF 1e9+7.
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1005;
int N, M, cnt;
double mp[maxN][maxN], lowercost[maxN];
bool vis[maxN];
struct node
{
    double x, y;
    node(double a=0, double b=0):x(a), y(b) {}
}a[maxN];
double Distance(int x, int y) { return sqrt( (a[x].x - a[y].x)*(a[x].x - a[y].x) + (a[x].y - a[y].y)*(a[x].y - a[y].y) ); }
void init()
{
    memset(vis, false, sizeof(vis));
}
double Prime(int pos)
{
    double ans = 0.;
    for(int i=1; i<=N; i++) lowercost[i] = mp[pos][i];
    vis[pos] = true;
    for(int line=1; line<N; line++)
    {
        double minn = INF;
        for(int i=1; i<=N; i++)
        {
            if(!vis[i] && lowercost[i]<minn)
            {
                minn = lowercost[i];
                pos = i;
            }
        }
        vis[pos] = true;
        ans += minn;
        for(int i=1; i<=N; i++) lowercost[i] = min(lowercost[i], mp[pos][i]);
    }
    return ans;
}
int main()
{
    while(scanf("%d%d", &N, &M)!=EOF)
    {
        init();
        for(int i=1; i<=N; i++)
        {
            scanf("%lf%lf", &a[i].x, &a[i].y);
            for(int j=1; j<i; j++) mp[i][j] = mp[j][i] = Distance(i, j);
        }
        for(int i=1; i<=M; i++)
        {
            int e1, e2;
            scanf("%d%d", &e1, &e2);
            mp[e1][e2] = mp[e2][e1] = 0.;
        }
        printf("%.2lf\n", Prime(1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/84454318
今日推荐