HDU - 1875畅通工程再续

#include<iostream>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stdio.h>
using namespace std;
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define V 1000+4
#define sc(t) scanf("%d",&t);
#define pow(a) ((a)*(a))
#define INF 2000.0
double cost[V][V];
int vis[V];
double minCost[V];
struct node{
    int x, y;
};
node vet[V];
int cnt;
int n;
int t;
void prim(){
    double sum = 0;
    for (int i = 1; i <= n; i++){
        minCost[i] = cost[1][i];
        vis[i] = 0;
    }
    cnt = n;
    minCost;
    while (cnt--){
        int marks = 0; double MIN = INF;
        for (int i = 1; i <= n; i++){
            if (!vis[i] && MIN > minCost[i]){
                MIN = minCost[i];
                marks = i;
            }
        }
        if (marks == 0)break;
        vis[marks] = 1;
        sum += minCost[marks];
        for (int i = 1; i <= n; i++){
            if (minCost[i] > cost[marks][i])minCost[i] = cost[marks][i];
        }
    }
    sum *= 100.0;
    if (cnt==-1)
    printf("%.1f\n", sum);
    else printf("oh!\n");
}
int main(){
    while (~scanf("%d", &t))
    {
        while (t--){
            scanf("%d", &n);
            for (int i = 1; i <= n; i++){
                scanf("%d%d", &vet[i].x, &vet[i].y);
            }
            for (int i = 1; i <= n; i++){
                for (int j = 1; j < i; j++){
                    double sum = sqrt(pow(vet[i].x - vet[j].x) + pow(vet[i].y - vet[j].y));
                    if (sum >= 10.0 && sum <= 1000.0)cost[i][j] = cost[j][i] = sum;
                    else cost[i][j] = cost[j][i] = INF;
                }
            }
            prim();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值