当前位置:网站首页>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
边栏推荐
- Exercise 8-8 moving letters
- 7-16 find the set of integers that meet the given conditions
- 7-6 mixed type data format input
- Canvas utility library fabric JS user manual
- 如何查询淘宝天猫的宝贝类目
- Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
- 7-22 tortoise and rabbit race (result oriented)
- Solr series of full-text search engines - basic principles of full-text search
- Leetcode(4)——寻找两个正序数组的中位数
- simpleParallax. JS (create poor visual effects for website pictures)
猜你喜欢
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
八大排序
7-8 overspeed judgment
JS Part III
Exercise 8-8 moving letters
Exercise 6-6 use a function to output an integer in reverse order
7-10 calculate salary
Exercise 7-6 count capital consonants
556. The next larger element III
X86 assembly language - Notes from real mode to protected mode
随机推荐
【7.3】146. LRU缓存机制
八大排序
MongoDB索引
7-19 check denomination (solve binary linear equation)
洛谷P4047 [JSOI2010]部落划分 题解
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
Accelerating strategy learning using parallel differentiable simulation
JS Part 2
超简单手机地图开发
战略、战术(和 OKR)
String substitution
Why don't I have a rookie medal
JS matrix zero
7-20 print 99 formula table (format output)
7-7 12-24 hour system
Although not necessarily the best, it must be the hardest!
天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪
Exercise 8-7 string sorting
洛谷P3065 [USACO12DEC]First! G 题解
使用并行可微模拟加速策略学习