HDU-1875-畅通工程再续-最小生成树

来源:互联网 发布:张大奕的淘宝店招人吗 编辑:程序博客网 时间:2024/05/19 10:37

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1875

题意:其实只要把那些小于10和大于1000的权值变成INF酒可以了

#include<cstdio>#include<cmath>#define INF 1000000000const int N=201;struct node{    double x,y;    int i;};node f[N];double G[N][N];double low[N];void calc(const node &a,const node &b){    double dis=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));    if(dis<10 || dis>1000 ) dis=INF;    G[a.i][b.i]=G[b.i][a.i]=dis;}double prim(int n){    double ans=0,min;    bool vis[N]={0};    int pos=0,i,j;    for(int i=0;i<n;++i)        low[i]=G[0][i];    vis[pos]=true;    for(i=1;i<n;++i ){        for(j=0,min=low[j],pos=j;j<n;++j)if(!vis[j]){            if(low[j]<min){                min=low[j];                pos=j;            }        }        ans+=min;        vis[pos]=true;        for(j=0;j<n;++j){            if(!vis[j]&&low[j]>G[pos][j]){                low[j]=G[pos][j];            }        }    }    return ans;}int main(){    int n,T;    scanf("%d",&T);    while(T--){    scanf("%d",&n);        for(int i=0;i<n;++i){            scanf("%lf %lf",                  &f[i].x,&f[i].y);            f[i].i=i;        }        for(int i=0;i<n;++i)            for(int j=0;j<n;++j)if(i==j)                G[i][j]=INF;        for(int j=0;j<n;++j)        for(int i=j+1;i<n;++i){            calc(f[j],f[i]);        }        double ans=prim(n);        if(ans>=INF)                    puts("oh!");        else        printf("%.1lf\n",ans*100);    }}


0 0