【BZOJ2597】【WC2007】剪刀石头布

本文介绍了一种利用网络流算法解决特定组合计数问题的方法。通过对原图进行转化,建立最小费用最大流模型,实现了高效求解。文章详细阐述了算法的设计思路与实现细节,并附上了完整的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目链接】

【思路要点】

  • 数据范围暗示网络流/线性规划,但直接建图不太可行,需要发掘题目性质。
  • 考虑一个原图三个点的子图,它共有3条边,\(2^{3}=8\)种形式,其中能对答案产生1的贡献的有2种。
  • 观察剩余6种形式,我们发现,它们都满足三个端点处分别是一入一出、两入、两出。
  • 如果我们把出现一次这6种形式之一看做对答案造成1点损失,那么把损失归结在“两出”点处,即$$Ans=\dbinom{N}{3}-\sum_{i=1}^{N}\dbinom{D_{i}}{2}(D_{i}表示点i的出度)$$
  • 而\(\dbinom{D_{i}}{2}=\sum_{i=1}^{D_{i}}(i-1)\),我们可以依次向每个点连\(N\)条容量为1,费用从0开始递增的边,再将每个点与边进行匹配(边与其出点匹配),运行最小费用最大流即可。
  • 时间复杂度\(O(MinimumCostFlow(N^{2},N^{2}))\)。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 105;
const int MAXP = 20005;
const int MAXQ = 1000005;
const int INF = 1e9;
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, m, ans;
int s, t, tot, flow, cost;
int dist[MAXP], path[MAXP];
int point[MAXN][MAXN], mp[MAXN][MAXN];
unsigned home[MAXP], rec[MAXN][MAXN];
struct edge {int dest, flow, cost; unsigned home; };
vector <edge> a[MAXP];
bool spfa() {
	static int q[MAXQ], l = 0, r = -1;
	static bool inq[MAXP];
	for (int i = 0; i <= r; i++)
		dist[q[i]] = INF;
	q[l = r = 0] = s, dist[s] = 0, inq[s] = true;
	while (l <= r) {
		int tmp = q[l++];
		for (unsigned i = 0; i < a[tmp].size(); i++)
			if (a[tmp][i].flow && dist[tmp] + a[tmp][i].cost < dist[a[tmp][i].dest]) {
				dist[a[tmp][i].dest] = dist[tmp] + a[tmp][i].cost;
				path[a[tmp][i].dest] = tmp;
				home[a[tmp][i].dest] = i;
				if (!inq[a[tmp][i].dest]) {
					inq[a[tmp][i].dest] = true;
					q[++r] = a[tmp][i].dest;
				}
			}
		inq[tmp] = false;
	}
	return dist[t] != INF;
}
void FlowPath() {
	int p = t, ans = INF;
	while (p != s) {
		ans = min(ans, a[path[p]][home[p]].flow);
		p = path[p];
	}
	flow += ans, cost += ans * dist[t], p = t;
	while (p != s) {
		a[path[p]][home[p]].flow -= ans;
		a[p][a[path[p]][home[p]].home].flow += ans;
		p = path[p];
	}
}
void addedge(int x, int y, int flow, int cost) {
	a[x].push_back((edge) {y, flow, cost, a[y].size()});
	a[y].push_back((edge) {x, 0, -cost, a[x].size() - 1});
}
int main() {
	read(n);
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
		read(mp[i][j]);
	tot = n;
	for (int i = 1; i <= n; i++)
	for (int j = i + 1; j <= n; j++)
		point[i][j] = point[j][i] = ++tot;
	s = 0; t = ++tot;
	for (int i = 1; i <= n; i++)
	for (int j = i + 1; j <= n; j++)
		addedge(point[i][j], t, 1, 0);
	for (int i = 1; i <= n; i++) {
		int cnt = 0;
		for (int j = 1; j <= n; j++)
			if (mp[i][j] == 1) cnt++;
		cost += cnt * (cnt - 1) / 2;
		for (int j = 1; j <= n; j++)
			if (mp[i][j] == 2) {
				addedge(s, i, 1, cnt++);
				addedge(i, point[i][j], 1, 0);
				rec[i][j] = a[i].size() - 1;
			}
	}
	for (int i = 0; i <= tot; i++)
		dist[i] = INF;
	while (spfa()) FlowPath();
	ans = n * (n - 1) * (n - 2) / 6 - cost;
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++)
			if (mp[i][j] == 2) printf("%d ", 1 - a[i][rec[i][j]].flow);
			else printf("%d ", mp[i][j]);
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值