当前位置:网站首页>洛谷P3236 [HNOI2014]画框 题解
洛谷P3236 [HNOI2014]画框 题解
2022-04-23 12:26:00 【q779】
洛谷P3236 [HNOI2014]画框 题解
题目链接:P3236 [HNOI2014]画框
题意:小 T 准备在家里摆放几幅画,为此他买来了 N N N 幅画和 N N N 个画框。为了体现他的品味,小 T 希望能合理地搭配画与画框,使得其显得既不过于平庸也不太违和。
对于第 i i i 幅画与第 j j j个画框的配对,小 T 都给出了这个配对的平凡度 A i , j A_{i, j} Ai,j与违和度 B i , j B_{i, j} Bi,j。整个搭配方案的总体不和谐度为每对画与画框平凡度之和与每对画与画框违和度的乘积。具体来说,设搭配方案中第 i i i 幅画与第 P i P_i Pi 个画框配对,则总体不和谐度为
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
小 T 希望知道通过搭配能得到的最小的总体不和谐度是多少。
q779在水文章
这道题和P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 几乎就是一道题
可以先看下这篇题解(
变化不是很大,基本上就是改一下邻接矩阵啥的
只不过算法换成了KM求完全二分图最大权完美匹配
时间复杂度大概在 O ( k n 4 ) O(kn^4) O(kn4) 左右, k k k 为一个小常数
多测不清空,爆零两行泪!!!!
说句题外话,dfs版本好像是 O ( n 4 ) O(n^4) O(n4) 的,建议不要写
代码如下
#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;
}
转载请说明出处
版权声明
本文为[q779]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_50332374/article/details/124356031
边栏推荐
- AD20补充笔记3—快捷键+持续更新
- SynchronousQueue 源码解析
- 一个平面设计师的异想世界|ONES 人物
- Solution of asynchronous clock metastability -- multi bit signal
- Markdown语法学习
- On using go language to create websocket service
- 对话PostgreSQL作者Bruce:“转行”是为了更好地前行
- 外包干了五年,废了...
- Pagoda panel command line help tutorial (including resetting password)
- Fastjson 2 来了,性能继续提升,还能再战十年
猜你喜欢
How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
After a circle, I sorted out this set of interview questions..
SSL证书退款说明
消息队列概述
A detailed explanation of head pose estimation [collection of good articles]
VMware virtual machines export hard disk vmdk files using esxi
QT draw text
NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
Worder font page font comparison table
VMware虚拟机使用esxi 导出硬盘vmdk文件
随机推荐
Source code analysis of synchronousqueue
Tips for installing MySQL service in windows11: Install / Remove of the Service denied
【Redis 系列】redis 学习十三,Redis 常问简单面试题
QT draw image
worder字体网页字体对照表
异步时钟亚稳态 的解决方案——多bit信号
S2-062 远程命令执行漏洞复现(cve-2021-31805)
魔域来了H5游戏详细图文架设教程
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
In idea Solution to the problem of garbled code in Chinese display of properties file
Markdown语法学习
Next.js 静态数据生成以及服务端渲染的方式
Zigbee之CC2530最小系统及寄存器配置(1)
Lesson 25 static member variables of classes
Master slave replication configuration of MySQL
Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿
QT draw text
天梯赛赛前练习
Step function of activation function