当前位置:网站首页>牛客2021暑期训练3-B-Black and white
牛客2021暑期训练3-B-Black and white
2022-07-15 15:11:00 【yl-9】
牛客2021暑期训练3-B-Black and white
题意
给定 n*m 的矩阵,每个小方块有一个被染黑的代价 c[i][j],若四个方块形成矩形,其中一个方块无需代价。求矩阵全部染黑代价的最小值
题解

对于⼀个位置 ,如果该格子是黑色,我们连⼀条 A[i] 到 B[j] 的边。绿色已染,红色未染,所以
A[3]–B[2],
A[5]–B[2],
A[5]–B[6],
当我们要染 (3,6) 时,可以发现 A[3], B[6]已经在一个集合了
显然用最小生成树做就可以了
代码
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=5e3+9;
int n,m,A,B,cnt,tot;
LL ans,a,b,c,d,p;
int f[N*2];
struct node
{
int x,y;
LL z;
node() {
}
node(int i,int j,int k) {
x=i; y=j; z=k;}
bool operator < (const node &u) const
{
return z<u.z;
}
}t[N*N];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
void Work()
{
for(int i=1;i<=n+m;i++)
f[i]=i;
sort(t+1,t+1+cnt);
for(int i=1;i<=cnt&&tot!=n+m-1;i++)
{
int x=find(t[i].x),y=find(t[i].y+n);
if(x!=y)
{
ans+=t[i].z;
tot++;
f[x]=y;
}
}
}
int main()
{
cin>>n>>m>>a>>b>>c>>d>>p;
while(A<n)
{
A++; B=0;
while(B<m)
{
B++;
a=(a*a*b+a*c+d)%p;
t[++cnt]=(node){
A,B,a};
}
}
Work();
cout<<ans<<endl;
return 0;
}
边栏推荐
猜你喜欢

全球云市场增势迅猛,数据安全进入法治化的强监管时代

栈的应用—

【Flutter--实战】Flutter 简介

创建线程的方式

I / II. Advanced part - MySQL logical architecture, performance and join

一文揭开海底捞分拆上市疑团,究竟有多大投资价值?

基于ABP实现DDD--聚合和聚合根实践

【SpaceNet】SN6:Multi-Sensor All-Weather Mapping

剑指Offer17-打印从1到最大的n位数-模拟

EN 1317-5 Road restraint system products - CE certification
随机推荐
40+倍提升,详解 JuiceFS 元数据备份恢复性能优化之路
Puge - Code Honghu team -vscade code formatting and sorting
LeetCode_滑动窗口_二分搜索_中等_713.乘积小于 K 的子数组
一/二、高级篇- Mysql逻辑架构、性能与JOIN
工程仪器振弦无线采集仪的采集数据发送方式及在线监测系统
动作捕捉协助中国电力科学研究院建立边云协同电力自主巡检系统
树和二叉树
清晰的理解大端和小端
角色授权---通过添加和删除一级菜单,完成二级菜单的添加和删除
栈的应用—
SN6多光谱和SAR数据融合
C#一个方法返回多个值建议收藏
u-boot之顶层Makefile分析(一)
nvidia-smi命令无法正常使用的解决方案
sentinel1.8.4 持久化nacos配置
牛客2021暑期训练4-E-Tree Xor
40 + times improvement, explain in detail how to optimize the performance of juicefs metadata backup and recovery
【SpaceNet】SN6:Multi-Sensor All-Weather Mapping
The concept and characteristics of network security grid are simple and popular
An article uncovers the mystery of Haidilao's spin off and listing. How much investment value is it?