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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!