题目链接:点击这里
题目大意:
给出一个
n
×
m
n\times m
n×m 的矩阵,每个点有一个权值
a
i
,
j
a_{i,j}
ai,j ,初始时全是白色,你可以花费
a
i
,
j
a_{i,j}
ai,j 来染黑对应的格点,如果一个矩形的四个顶点有三个顶点已经被染黑,那么剩下的一个顶点可以无花费染黑,现在求染黑所有格点所需要的最小代价
题目分析:
不难想到我们最少选择
n
+
m
−
1
n+m-1
n+m−1 个点就可以将整个矩阵染黑,每一行每一列都至少有一个点被染黑即可
我们考虑对于两行两列会有
4
4
4 个坐标,有三个坐标被染黑第四个就可以不用染黑,所以我们可以从每一行每一列都至少有一个点被染黑入手,我们将行和列抽象成点,染黑一个点相当于连起来一个横行和一个纵行,这样问题就转化成了求此图的最小生成树,这个图有
n
+
m
n+m
n+m 个点,所以加
n
+
m
−
1
n+m-1
n+m−1 条边就可以了
注意
n
≤
5
e
3
n\le 5e3
n≤5e3 所以如果Kruskal算法用
s
o
r
t
sort
sort 的话时间复杂度是
O
(
n
2
log
n
2
)
O(n^2\log n^2)
O(n2logn2) ,是会超时的,需要使用桶排来优化掉
log
\log
log
具体细节见代码:
Code1 Kruskal算法+桶排(时间复杂度为
O
(
n
2
)
O(n^2)
O(n2) )
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 5e3+5;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
int n,m,a,b,c,d,p,cnt,fa[maxn<<1];
int find(int x)
{
return fa[x]==x ? x : fa[x] = find(fa[x]);
}
vector<pair<int,int>>edge[maxn*30];
signed main()
{
for(int i = 0;i < maxn<<1;i++) fa[i] = i;
n = read(),m = read(),a = read(),b = read(),c = read(),d = read(),p = read();
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
a = (a*a*b+a*c+d)%p;
edge[a].push_back({i,j+n});
}
}
int ans = 0;
for(int i = 0;i < p;i++)
for(auto it:edge[i])
{
int fax = find(it.first),fay = find(it.second);
if(fax != fay)
{
fa[fax] = fay;
ans += i;
}
}
cout<<ans<<endl;
return 0;
}
Code2 Prim算法(时间复杂度为 O ( n 2 ) O(n^2) O(n2))
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
//#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 5e3+5;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
int n,m,a,b,c,d,p,g[maxn<<1][maxn<<1],dis[maxn];
bool vis[maxn];
signed main()
{
memset(g,0x3f,sizeof(g));
memset(dis,0x3f,sizeof(dis));
n = read(),m = read(),a = read(),b = read(),c = read(),d = read(),p = read();
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= m;j++)
{
a = ((ll)a*a*b+(ll)a*c+d)%p;
g[i][j+n] = g[j+n][i] = a;
}
}
ll ans = 0; dis[1] = 0;
for(int i = 0;i < n+m;i++)
{
int pos = -1;
for(int j = 1;j <= n+m;j++)
if(!vis[j] && (pos==-1||dis[j]<dis[pos])) pos = j;
vis[pos] = true;ans += dis[pos];
for(int j = 1;j <= n+m;j++)
dis[j] = min(dis[j],g[pos][j]);
}
cout<<ans<<endl;
return 0;
}