最小生成树Prim模板

Prim适用于稠密图,对于给定坐标求最小生成树的题目再合适不过。

e.g. 

畅通工程再续 http://acm.hdu.edu.cn/showproblem.php?pid=1875

Freckles http://poj.org/problem?id=2560

这里贴出Hdu 1875 的代码


#include "iostream"
#include "stdio.h"
#include "math.h"
#include "map"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define N 105
#define INF 1<<30

struct NODE
{
	double x,y;
};

NODE island[N];
bool visited[N];
double dis[N][N];
int S_set[N];

double DIS(NODE a,NODE b)
{
    double x= sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
    if(x<10||x>1000)
        return INF;
    return x;
}

int main()
{
    int cas,i,j,n,k;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%lf%lf",&island[i].x,&island[i].y);
        memset(dis,-1,sizeof(dis));
        for(i=1;i<=n;i++)
            for(j=1;jdis[cur_p][k]&&dis[cur_p][k]>0&&!visited[k])
                    {
                        minn=dis[cur_p][k];
                        point=k;
                    }
                }
            }
            if(minn==INF)
            {
                flag=false;
                break;
            }
            ans+=minn;
            S_set[i]=point;
            visited[S_set[i]]=true;
        }
        if(!flag){
            cout<<"oh!"<


你可能感兴趣的:(C/C++,Data,Structure,ACM,struct,path)