当前位置:网站首页>Luogu p4047 [jsoi2010] tribal division solution
Luogu p4047 [jsoi2010] tribal division solution
2022-07-03 14:27:00 【q779】
Luogu P4047 [JSOI2010] Tribal Division Answer key
Topic link :P4047 [JSOI2010] Tribal Division
The question :
Congcong's research found that , Wild islanders always live in groups , however , Not all the savages on the whole desert island belong to the same tribe , Wild people are always ganging up to form their own tribes , There are often fights between different tribes . It's just , It's all a mystery —— Congcong doesn't know how the tribes are distributed .
But the good news is , Congcong gets a map of the desert island . The map is marked n n n A place where savages live ( It can be regarded as coordinates on the plane ). We know , Savages of the same tribe always live nearby . We put the distance between the two tribes , It is defined as the distance between the two nearest settlements in the tribe . Congcong also got a meaningful message —— These savages are divided into k k k Tribes ! That's good news . Congcong hopes to mine the details of all tribes from this information . He is trying such an algorithm :
For any method of tribal division , Can find the distance between the two tribes , Congcong hopes to find a way to divide tribes , Keep the two nearest tribes as far away as possible .
for example , The left figure below shows a good division , The picture on the right is not . Please program to help Congcong solve this problem .
about 100 % 100\% 100% The data of , Guarantee 2 ≤ k ≤ n ≤ 1 0 3 2 \leq k \leq n \leq 10^3 2≤k≤n≤103, 0 ≤ x , y ≤ 1 0 4 0 \leq x, y \leq 10^4 0≤x,y≤104.
The minimum distance is the maximum , One eye and two points
But here is a kruskal Solution method
Let's put the O ( n 2 ) O(n^2) O(n2) The edge is built
Then find the minimum spanning tree
Greedily divide the minimum spanning tree into k k k A connecting block
Concrete , We just merge the smallest edges of the tree in turn
Finally, a total of ( n − 1 ) − ( k − 1 ) = n − k (n-1)-(k-1) = n-k (n−1)−(k−1)=n−k side
So the answer is n − k + 1 n-k+1 n−k+1 The edge weight of the edge
Mouth paste a proof of correctness ( Maybe not very rigorous )
Consider counter evidence
Set an edge on the minimum spanning tree w 1 ( u , v ) w_1(u,v) w1(u,v) , Replace it with w 2 ( u , v ′ ) w_2(u,v^{\prime}) w2(u,v′) ( w 2 > w 1 w_2>w_1 w2>w1)
If w 1 w_1 w1 Will be merged and we merged w 2 w_2 w2 , It seems that there is no change
If w 1 w_1 w1 Will be merged and we didn't merge w 2 w_2 w2 ,
Then we must merge a ratio w 1 w_1 w1 Big side , And it is the edge on the minimum spanning tree
So the answer will be too big
If w 1 w_1 w1 Will not be merged and we merged w 2 w_2 w2 , There is no such situation .
If w 1 w_1 w1 Will not be merged and we did not merge w 2 w_2 w2 , No influence .
Time complexity O ( n 2 log n ) O(n^2 \log n) O(n2logn)
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(1e3+15)
#define M (int)(1e6+15)
#define pf(x) ((x)*(x))
struct Edge{
int u,v,w;}e[M];
int n,k,f[N],x[N],y[N],c[N],pos;
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);}
void merge(int u,int v){
f[find(u)]=find(v);}
void addEdge(int u,int v,int w){
e[++pos]={
u,v,w};}
int dis(int i,int j){
return pf(x[i]-x[j])+pf(y[i]-y[j]);}
void kruskal()
{
sort(e+1,e+1+pos,[](Edge a,Edge b)
{
return a.w<b.w;
});
int cnt=0;
for(int i=1; i<=pos&&cnt<n; i++)
{
int u=e[i].u,v=e[i].v;
if(find(u)!=find(v))
{
merge(u,v);
c[++cnt]=e[i].w;
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n >> k;
for(int i=1; i<=n; i++)
f[i]=i,cin >> x[i] >> y[i];
for(int i=1; i<=n; i++)
for(int j=1; j<i; j++)
addEdge(i,j,dis(i,j));
kruskal();
cout << fixed << setprecision(2);
cout << sqrt((double)c[n-k+1]) << '\n';
return 0;
}
Reprint please explain the source
边栏推荐
- Leetcode (4) -- find the median of two positively ordered arrays
- 洛谷P5536 【XR-3】核心城市 题解
- String sort
- Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
- Why don't I have a rookie medal
- 八大排序
- X86 assembly language - Notes from real mode to protected mode
- LNMP环境mail函数不能发送邮件解决
- 中国PETG市场预测及战略研究报告(2022版)
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
猜你喜欢

玖逸云黑免费无加密版本源码

Doris学习笔记之数据表的创建

7-8 overspeed judgment

Exercise 10-8 recursive implementation of sequential output of integers

MySQL multi table query subquery

Exercise 7-6 count capital consonants

Exercise 8-7 string sorting

Print. JS -- web page file printing

Exercise 10-6 recursively find Fabonacci sequence

7-11 calculation of residential water charges by sections
随机推荐
Find the sum of the elements of each row of the matrix
fpga阻塞赋值和非阻塞赋值
puzzle(016.3)千丝万缕
Sendmail无法发送邮件及发送过慢解决
Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
Exercise 6-2 using functions to sum special A-string sequences
Exercise 9-1 time conversion
556. The next larger element III
Exercise 8-7 string sorting
Leetcode(4)——寻找两个正序数组的中位数
7-9 find a small ball with a balance
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Exercise 7-6 count capital consonants
Exercise 8-8 moving letters
Exercise 10-2 recursive factorial sum
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
Protobuf and grpc
分布式事务(Seata) 四大模式详解
Sword finger offer 28 Symmetric binary tree
concat和concat_ws()区别及group_concat()和repeat()函数的使用
