当前位置:网站首页>Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
2022-04-23 12:34:00 【q779】
Luogu P5540 [BalkanOI2011] timeismoney | Minimum product spanning tree Answer key
Topic link :P5540 [BalkanOI2011] timeismoney | Minimum product spanning tree
The question : Give a n n n A little bit m m m Undirected graph of strip and edge , The first i i i An edge has two weights a i a_i ai and b i b_i bi .
Find a spanning tree of the graph T T T , bring
( ∑ e ∈ T a e ) × ( ∑ e ∈ T b e ) \left(\sum_{e\in T}a_e\right)\times\left(\sum_{e\in T}b_e\right) (e∈T∑ae)×(e∈T∑be)Minimum
Notice that for a spanning tree , Both sides of the equation are constants
Consider mapping the spanning tree to points on the plane ( x , y ) (x,y) (x,y)
among x = ∑ e ∈ T a e , y = ∑ e ∈ T b e x=\sum\limits_{e\in T}a_e,y=\sum\limits_{e\in T}b_e x=e∈T∑ae,y=e∈T∑be
Then the problem turns into finding a point ( x , y ) (x,y) (x,y) bring x × y x\times y x×y Minimum
It can be found that the distribution of these points has boundaries
namely x x x Particularly large y y y Especially when I was young and y y y Particularly large x x x When I was very young
These two cases correspond to points A A A and B B B
Then there are A A A For distance x x x The closest point of the axis , B B B For distance y y y The closest point of the axis
For straight lines A B AB AB , Clearly in A B AB AB The above is not an optimal solution
According to the properties of inverse proportional function , It can be found that this solution must be in A , B A,B A,B On the lower convex hull of the end point
Because we can't enumerate all spanning trees and find two-dimensional convex hull
Consider solving recursively , I don't know if you know about circumcision , It doesn't matter if I don't know
This solution is a bit like circle cutting
First find the distance line A B AB AB The farthest point C C C ( stay A B AB AB below )
Then a recursive A C AC AC and C B CB CB , You can find the solution
How to find C C C How about it? ? It can be found that at this time S △ A B C S_{\triangle ABC} S△ABC The maximum value is taken
and S △ A B C = − 1 2 A B * × A C * S_{\triangle ABC} = -\dfrac{1}{2}\overset{\longrightarrow}{AB}\times \overset{\longrightarrow}{AC} S△ABC=−21AB*×AC*
( notes : What is written here is not very standard , It should actually be divided by a unit vector z \boldsymbol{z} z Of , It doesn't matter )
Push persimmon link qwq
A B * × A C * = ( x B − x A ) ( y C − y A ) − ( y B − y A ) ( x C − x A ) = ( x B − x A ) × y C + ( y A − y B ) × x C + y B x A − x B y A \begin{aligned} \overset{\longrightarrow}{AB}\times \overset{\longrightarrow}{AC} &= (x_B-x_A)(y_C-y_A)-(y_B-y_A)(x_C-x_A)\\ &=(x_B-x_A)\times y_C + (y_A-y_B)\times x_C + y_Bx_A-x_By_A \end{aligned} AB*×AC*=(xB−xA)(yC−yA)−(yB−yA)(xC−xA)=(xB−xA)×yC+(yA−yB)×xC+yBxA−xByA
It can be found that the latter two items are fixed values
Therefore, we set the edge right to ( x B − x A ) b i + ( y A − y B ) a i (x_B-x_A)b_i+(y_A-y_B)a_i (xB−xA)bi+(yA−yB)ai , Then run the minimum spanning tree
because n n n Very small , And the analysis of the expected number of points on the convex hull is very complex
The complexity of this problem can be approximately regarded as O ( k m log m ) O(km\log m) O(kmlogm), among k k k Is a small constant , Is said to be ln n \sqrt{\ln n} lnn , Don't know much about it
The code is as follows
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define gc() readchar()
#define pc(a) putchar(a)
#define SIZ (int)(1e5+15)
char buf1[SIZ],*p1,*p2;
char readchar()
{
if(p1==p2)p1=buf1,p2=buf1+fread(buf1,1,SIZ,stdin);
return p1==p2?EOF:*p1++;
}
template<typename T>void read(T &k)
{
char ch=gc();T x=0,f=1;
while(!isdigit(ch)){
if(ch=='-')f=-1;ch=gc();}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
k=x*f;
}
template<typename T>void write(T k)
{
if(k<0){
k=-k;pc('-');}
static T stk[66];T top=0;
do{
stk[top++]=k%10,k/=10;}while(k);
while(top){
pc(stk[--top]+'0');}
}
#define N (int)(2e2+15)
#define M (int)(1e4+15)
int n,m;
struct Edge
{
int u,v,a,b,w,next;
}e[M];
struct vct
{
int x,y;
}ans;
int f[N];
vct operator-(vct a,vct b){
return (vct){
a.x-b.x,a.y-b.y};}
int cross(vct a,vct b){
return a.x*b.y-a.y*b.x;}
void init(){
for(int i=1; i<=n; i++) f[i]=i;}
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);}
void merge(int u,int v){
u=find(u);v=find(v);f[u]=v;}
vct kruskal()
{
sort(e+1,e+1+m,[](Edge a,Edge b){
return a.w<b.w;
});
init();
vct res={
0,0};
int cnt=0;
for(int i=1; i<=m; i++)
{
int u=find(e[i].u),v=find(e[i].v);
if(u!=v)
{
merge(u,v);
res.x+=e[i].a;
res.y+=e[i].b;
if(++cnt==n-1)break;
}
}
int Ans=ans.x*ans.y,Res=res.x*res.y;
if(Ans>Res||Ans==Res&&ans.x>res.x)ans=res;
return res;
}
void solve(vct A,vct B)
{
for(int i=1; i<=m; i++)
e[i].w=e[i].b*(B.x-A.x)+e[i].a*(A.y-B.y);
vct C=kruskal();
if(cross(B-A,C-A)>=0)return;
solve(A,C);solve(C,B);
}
signed main()
{
read(n);read(m);
ans={
0x3f3f3f3f,0x3f3f3f3f};
for(int i=1; i<=m; i++)
{
read(e[i].u);read(e[i].v);
read(e[i].a);read(e[i].b);
e[i].u++;e[i].v++;
}
for(int i=1; i<=m; i++)
e[i].w=e[i].a;
vct A=kruskal();
for(int i=1; i<=m; i++)
e[i].w=e[i].b;
vct B=kruskal();
solve(A,B);
printf("%lld %lld\n",ans.x,ans.y);
return 0;
}
Reprint please explain the source
版权声明
本文为[q779]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231226043383.html
边栏推荐
- SQL exercise (I)
- [redis series] redis learning 13. Redis often asks simple interview questions
- 网络信息安全之零信任
- 【unity笔记】L4Unity中的基础光照
- IDEA 数据库插件Database Navigator 插件
- Next.js 静态数据生成以及服务端渲染的方式
- STM32CubeProgrammer基础使用说明
- AD20补充笔记3—快捷键+持续更新
- 万事有你 未来可期 | ONES 2022校园招聘正式开启
- I changed to a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings
猜你喜欢
实现一个盒子在父盒子中水平垂直居中的几种“姿势”
S2-062 remote command execution vulnerability recurrence (cve-2021-31805)
免费试用一个月的服务器,并附上教程
【每日一题】棋盘问题
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
On lambda powertools typescript
Metalama简介4.使用Fabric操作项目或命名空间
NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
【vulnhub靶场】-dc2
AI video cloud vs narrowband HD, who is the darling of the video era
随机推荐
【每日一题】棋盘问题
QT interprocess communication
【unity笔记】L4Unity中的基础光照
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
洛谷P3236 [HNOI2014]画框 题解
IDEA 代码质量规范插件SonarLint
宝塔面板命令行帮助教程(包含重置密码)
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
Qt一个进程运行另一个进程
为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
第二十三课 临时对象
The maximum number of remote desktop servers has been exceeded
传统企业如何应对数字化转型?这些书给你答案
第二十五课 类的静态成员变量
Next.js 静态数据生成以及服务端渲染的方式