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

时间:2021-08-12
本文章向大家介绍 2021“MINIEYE杯”中国大学生算法设计超级联赛(8)1003. Ink on paper(裸最小生成树),主要包括 2021“MINIEYE杯”中国大学生算法设计超级联赛(8)1003. 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

就是求最小生成树的最大边。由于输出的是时间的平方,且两个圆扩散的速度分别为0.5,这样两圆相遇的时间实际上就是两个点的边权值。因为输出的是时间的平方所以答案就限制在了整数域里。注意即使一棵树有多棵最小生成树,但最大边的边权值是一样的。这个题n为5e3且为完全图,自然选择prim,常数大的kruscal必然会超时。一开始还写了一波二分+并查集想必也是会t...注意极限数据范围已经大于1e18了,inf必须要设置为9e18或者用__int128才可以(比赛的时候没注意wa了一发

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,x[5005], y[5005];
int a[5005][5005], d[5005];
bool v[5005];
void prim()
{   
    for(int i = 0; i <= n; i++) v[i] = 0;
    for(int i = 1; i <= n; i++) d[i] = 9e18;
    d[1] = 0; 
    int mx = 0;
    for(int i = 1; i < n; i++) {
        int x = 0;
        for(int j = 1; j <= n; j++)
        {
            if(!v[j] && (x == 0 || d[j] < d[x])) {
                x=j;
            }
        }
        v[x]=1;
        for(int y = 1; y <= n; y++)
        {
            if(!v[y]) {
                d[y] = min(d[y], a[x][y]);
            }
        }
    }
    for(int i = 1; i <= n; i++) mx = max(mx, d[i]);
    printf("%lld\n", mx);
}
signed main()
{
    int t;
    scanf("%lld", &t);
    while(t--) {
        scanf("%lld", &n);
        for(int i = 1;i <= n; i++) {
            scanf("%lld%lld", &x[i], &y[i]);
        }
        for(int i = 1; i <= n; i++) {
            for(int j=i;j<=n;j++) {
                a[i][j] = a[j][i] = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
            }
        }
        prim();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lipoicyclic/p/15134855.html