当前位置:网站首页>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
边栏推荐
- Fastjson 2 来了,性能继续提升,还能再战十年
- 九十八、freemarker框架报错 s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request
- 大家帮我看一下这是啥情况,MySQL5.5的。谢了
- BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
- BUUCTF WEB [BUUCTF 2018]Online Tool
- On lambda powertools typescript
- MySQL function - recursive function
- 软件测试基础DAY2-用例执行
- Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
- QT draw image
猜你喜欢
随机推荐
传统企业如何应对数字化转型?这些书给你答案
SSL证书退款说明
CGC: contractual graph clustering for community detection and tracking
大家帮我看一下这是啥情况,MySQL5.5的。谢了
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?
IDEA设置版权信息
MySQL函数-递归函数
XinChaCha Trust SSL Organization Validated
【unity笔记】L4Unity中的基础光照
Running error: unable to find or load the main class com xxx. Application
SQL 练习(一)
Pre competition practice of TIANTI competition
栈和队列a
Metalama简介4.使用Fabric操作项目或命名空间
How to switch PHP version in Windows 2008 system
AD20补充笔记3—快捷键+持续更新
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
BUUCTF WEB [BJDCTF2020]The mystery of ip
标签与路径
Qt双缓冲绘图