您的位置:首页 > 其它

【BZOJ2597】【WC2007】剪刀石头布

2018-01-17 10:05 381 查看
【题目链接】

点击打开链接

【思路要点】

数据范围暗示网络流/线性规划,但直接建图不太可行,需要发掘题目性质。
考虑一个原图三个点的子图,它共有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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: