2021“MINIEYE杯”中国大学生算法设计超级联赛(8)1003 Ink on paper

4 篇文章 0 订阅

Ink on paper

Problem Description

Bob accidentally spilled some drops of ink on the paper. The initial position of the i-th drop of ink is (xi,yi), which expands outward by 0.5 centimeter per second, showing a circle.

The curious Bob wants to know how long it will take for all the inks to become connected. In order to facilitate the output, please output the square of the time.

Input

The first line of input contains one integer T(1≤T≤5), indicating the number of test cases.

For each test case, the first line contains one integer n(2≤n≤5000), indicating the number of ink on the paper.

Each of the next n lines contains 2 integers (xi,yi)(|xi|≤109,|yi|≤109), indicating that x and y coordinates of the ink.

Output

For each test case, output one line containing one decimal, denoting the answer.

Sample Input

2
3
0 0
1 1
0 1
5
1 1
4 5
1 4
2 6
3 10

Sample Output

1
17

题目大意:

有n滴墨水滴在纸面上,每滴墨水都以0.5cm的速度以圆形扩散,给定每一滴墨水的坐标,求经过多久所有的墨水可以连接在一起。

为了方便输出,输出所需时间的平方。

题解:

将任意两个墨水滴连接并求解最小生成树就是墨水恰好全部连接时的情况,全部连接的时间就是最小生成树中最长的那条边形成的时间,在求距离的时候无需开方,直接除以(0.5)^2即可得出答案。

注意考虑在没有墨水点或墨水点位置只有一种的情况下输出0。

AC代码:

#include<cstring>
#include<iostream>  
#include<cmath>
#include<algorithm> 
using namespace std;  
#define MAX 5050  
typedef long long ll;
const ll MAXCOST = 8000000000000000001;
struct node {
    ll x, y;
    int num;
} a[MAX];   
ll graph[MAX][MAX];   
ll prim(ll graph[][MAX], int n) { // prim求解最小生成树 
    ll lowcost[MAX];
    ll mst[MAX];
    ll i, j, min, minid;
    ll sum = 0;  
    for (i = 2; i <= n; i ++) {  
        lowcost[i] = graph[1][i];
        mst[i] = 1;
    }  
    mst[1] = 0;  
    for (i = 2; i <= n; i ++) {  
        min = MAXCOST;  
        minid = 0;  
        for (j = 2; j <= n; j ++) {  
            if (lowcost[j] < min && lowcost[j]) {  
                min = lowcost[j];  
                minid = j;  
            }  
        }      
        sum = max(sum, min);  
        lowcost[minid] = 0;  
        for (j = 2; j <= n; j ++) {  
            if (graph[minid][j] < lowcost[j]) {  
                lowcost[j] = graph[minid][j];  
                mst[j] = minid;            
            }  
        }  
    } 
    return sum;  
}  
int main() {  
    int m, n;  
    int t;
    cin >> t;
    while (t --) {
        memset(graph, 0, sizeof graph); 
        cin >> n;
        for (int i = 1; i <= n; i ++) {
            cin >> a[i].x >> a[i].y;
            a[i].num = i;
        }
        for (int i = 1; i <= n; i ++) { // 将任意两个墨水用边连接,边权为两点间距离 
            for (int j = i; j <= n; j ++) {
                graph[j][i] = graph[i][j] = (pow(a[i].x-a[j].x, 2) + pow(a[i].y - a[j].y, 2));
            }
        }
        ll cost = prim(graph, n); 
        if (cost == MAXCOST) cout << "0\n"; // 墨水点不同位置数量小于等于1时 
        else cout << cost << '\n';
    }     
    return 0;  
} 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值