当前位置:网站首页>Luogu p3236 [hnoi2014] picture frame solution
Luogu p3236 [hnoi2014] picture frame solution
2022-04-23 12:34:00 【q779】
Luogu P3236 [HNOI2014] Picture frame Answer key
Topic link :P3236 [HNOI2014] Picture frame
The question : Small T I'm going to put some pictures at home , For this reason, he bought N N N Painting and N N N A picture frame . To reflect his taste , Small T I hope I can reasonably match the picture and frame , Make it neither too mediocre nor too contrary .
For the first i i i The painting is similar to j j j Pairing of picture frames , Small T Both give the triviality of this pairing A i , j A_{i, j} Ai,j And disobedience B i , j B_{i, j} Bi,j. The sum of the total matching degree of each picture frame and the non-uniformity of the whole picture frame is the product of the total matching degree of each picture frame and the non-uniformity of the whole picture frame . say concretely , Set... In the matching scheme i i i The painting is similar to P i P_i Pi Frame pairing , The overall disharmony is
d i s h a r m o n y = ∑ i = 1 N A i , p i × ∑ i = 1 N B i , p i \mathrm{disharmony}=\sum_{i=1}^{N}A_{i,p_i}\times \sum_{i=1}^{N}B_{i,p_i} disharmony=i=1∑NAi,pi×i=1∑NBi,pi
Small T I hope to know the minimum overall disharmony that can be obtained through collocation .
q779 In water article
This question and P5540 [BalkanOI2011] timeismoney | Minimum product spanning tree It's almost a problem
You can read this first Answer key (
The change is not great , It's basically changing the adjacency matrix
It's just that the algorithm is changed to KM Find the maximum weight perfect match of complete bipartite graph
The time complexity is about O ( k n 4 ) O(kn^4) O(kn4) about , k k k Is a small constant
It's hard to empty , Burst two lines of tears !!!!
Just as an aside ,dfs The version seems to be O ( n 4 ) O(n^4) O(n4) Of , It is recommended not to write
The code is as follows
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(75)
int n,g[N][N],a[N][N],b[N][N],Q;
struct vct
{
int x,y;
}ans;
int slack[N],lx[N],ly[N],px[N],py[N],pre[N],d,vx[N],vy[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 aug(int v)
{
int t;
while(v)
{
t=px[pre[v]];
px[pre[v]]=v;
py[v]=pre[v];
v=t;
}
}
queue<int> q;
void bfs(int s)
{
for(int i=1; i<=n; i++)
slack[i]=INF,vx[i]=vy[i]=0;
while(!q.empty())q.pop();
q.push(s);
while(1)
{
while(!q.empty())
{
int u=q.front();q.pop();
vx[u]=1;
for(int i=1; i<=n; i++)
{
if(!vy[i]&&lx[u]+ly[i]-g[u][i]<slack[i])
{
slack[i]=lx[u]+ly[i]-g[u][i];
pre[i]=u;
if(!slack[i])
{
vy[i]=1;
if(!py[i]){
aug(i);return;}
else q.push(py[i]);
}
}
}
}
int d=INF;
for(int i=1; i<=n; i++)
if(!vy[i])d=min(d,slack[i]);
for(int i=1; i<=n; i++)
{
if(vx[i])lx[i]-=d;
if(vy[i])ly[i]+=d;
else slack[i]-=d;
}
for(int i=1; i<=n; i++)
{
if(!vy[i]&&!slack[i])
{
vy[i]=1;
if(!py[i]){
aug(i);return;}
else q.push(py[i]);
}
}
}
}
vct KM()
{
vct res={
0,0};
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
lx[i]=max(lx[i],g[i][j]);
for(int i=1; i<=n; i++)bfs(i);
for(int i=1; i<=n; i++)
{
res.x+=a[py[i]][i];
res.y+=b[py[i]][i];
}
if(res.x*res.y<ans.x*ans.y)ans=res;
for(int i=1; i<=n; i++)
px[i]=py[i]=lx[i]=ly[i]=pre[i]=0;
return res;
}
void solve(vct A,vct B)
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
g[i][j]=-b[i][j]*(B.x-A.x)-a[i][j]*(A.y-B.y);
vct C=KM();
if(cross(B-A,C-A)>=0)return;
solve(A,C);solve(C,B);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> Q;
while(Q--)
{
cin >> n;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
cin >> a[i][j];
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
cin >> b[i][j];
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
g[i][j]=-a[i][j];
vct A=KM();
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
g[i][j]=-b[i][j];
vct B=KM();
ans=(A.x*A.y<B.x*B.y)?A:B;
solve(A,B);
cout << ans.x*ans.y << endl;
}
return 0;
}
Reprint please explain the source
版权声明
本文为[q779]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231226043342.html
边栏推荐
- 一个平面设计师的异想世界|ONES 人物
- QT double buffer drawing
- 为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
- 网络信息安全之零信任
- Pagoda panel command line help tutorial (including resetting password)
- PHP generates JSON to process Chinese
- Solution of asynchronous clock metastability -- multi bit signal
- QT redraw events and cuts
- STM32控制步进电机(ULN2003+28byj)
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
猜你喜欢
![[redis series] redis learning 13. Redis often asks simple interview questions](/img/4c/2440af6daa26a0ca6cb64f32bf138e.png)
[redis series] redis learning 13. Redis often asks simple interview questions

Fastjson 2 来了,性能继续提升,还能再战十年

mysql中 innoDB执行过程分析

Please help me see what this is, mysql5 5. Thanks

Idea setting copyright information

XinChaCha Trust SSL Organization Validated

面了一圈,整理了这套面试题。。

Everything can be expected in the future | one 2022 campus recruitment officially opened

0基础可以考CPDA数据分析师证书吗

Next. JS static data generation and server-side rendering
随机推荐
MySQL function - recursive function
Plato Farm-以柏拉图为目标的农场元宇宙游戏
On using go language to create websocket service
亿级流量架构,服务器如何扩容?写得太好了!
Worder font page font comparison table
QT interprocess communication
Outsourcing for five years, abandoned
画结果图推荐网址
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
SynchronousQueue 源码解析
0基础可以考CPDA数据分析师证书吗
The maximum number of remote desktop servers has been exceeded
SQL exercise (I)
洛谷P3236 [HNOI2014]画框 题解
网站首页文件被攻击篡改的形式有哪些
Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
SQL 练习(一)
How do programmers finalize nucleic acid statistics with 130 lines of code
A graphic designer's fantasy world | ones characters
After a circle, I sorted out this set of interview questions..