UVa 10034 Freckles Prim算法

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <memory.h>

using namespace std;
const int MAX=105;
const double INF=1<<30;
bool  vis[MAX],standard[MAX];
int n,setNum;
double key[MAX],line[MAX][2],map[MAX][MAX];
double Distance(const int &i,const int &j){
    return pow(pow((line[i][0]-line[j][0]),2)+
               pow((line[i][1]-line[j][1]),2),0.5);
}
void solve(){
    int u;
    double minVal;
    double ans=0;
    for(int i=1;i<=n;i++)
        key[i]=INF;
    key[1]=0;
    while(memcmp(vis,standard,sizeof(vis))){
        //cout<<"u"<<endl;
        minVal=INF;
        for(int i=1;i<=n;i++){
            if(!vis[i]&&key[i]<minVal){
                minVal=key[i];
                u=i;
            }
        }
        ans+=key[u];
        vis[u]=1;
        for(int i=1;i<=n;i++){
            if(i==u)
                continue;
            if(!vis[i]&&map[u][i]<key[i])
                key[i]=map[u][i];
        }
    }
    printf("%.2lf/n",ans);
    return ;
}
int main()
{
    freopen("i.txt","r",stdin);
    freopen("e://t2.txt","w",stdout);
    int nCase,blank=0;
    cin>>nCase;
    while(nCase--){
        if(blank++)
            cout<<endl;
        memset(vis,0,sizeof(vis));
        memset(key,0,sizeof(key));
        memset(standard,0,sizeof(standard));
        memset(map,0,sizeof(map));
        cin>>n;
        for(int i=1;i<=n;i++)
            standard[i]=1;
        for(int i=1;i<=n;i++)
            cin>>line[i][0]>>line[i][1];
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                map[i][j]=map[j][i]=Distance(i,j);
        solve();
        /*for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cout<<map[i][j]<<"            ";
            }
            cout<<endl;
        }*/
    }
    return 0;
}

 

你可能感兴趣的:(算法,include,distance)