2597: [Wc2007]剪刀石头布

链接

分析:  

  费用流。

  首先转化一下问题,整张图最优的情况是存在$C_n^3$个,即任意3个都行,然后考虑去掉最少不满足的三元环。

  如果u赢了v,u向v连一条边,如果v有k条入边,那么说明少了$C_k^2$个三元环,所对每场比赛分配度数,求最小费用最大流。

  具体地:S向每场比赛连容量为1,花费为0的边;每场比赛向两个人连容量为1,花费为0的边;每个人因为度数不同,花费不同,所以差分后建边。

  还有一种随机化+迭代的做法。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = , INF = 1e9;
struct Edge { int from, to, nxt, cap, cost; } e[];
int head[N], pre[N], dis[N], q[], deg[N], id[][], A[][], P[N], tag[N], En = , S, T;
bool vis[N]; inline void add_edge(int u,int v,int f,int w) {
++En; e[En].from = u, e[En].to = v, e[En].cap = f, e[En].cost = w, e[En].nxt = head[u]; head[u] = En;
++En; e[En].from = v, e[En].to = u, e[En].cap = , e[En].cost = -w, e[En].nxt = head[v]; head[v] = En;
}
bool spfa() {
for (int i = ; i <= T; ++i) dis[i] = INF, vis[i] = false, pre[i] = ;
int L = , R = ;
q[++R] = S, vis[S] = true, dis[S] = ;
while (L <= R) {
int u = q[L ++]; vis[u] = false;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v] > dis[u] + e[i].cost && e[i].cap > ) {
dis[v] = dis[u] + e[i].cost;
pre[v] = i;
if (!vis[v]) q[++R] = v, vis[v] = true;
}
}
}
return dis[T] != INF;
}
int mcf() {
int Cost = , Flow = ;
while (spfa()) {
int now = 1e9;
for (int i = T; i != S; i = e[pre[i]].from)
now = min(now, e[pre[i]].cap);
for (int i = T; i != S; i = e[pre[i]].from)
e[pre[i]].cap -= now, e[pre[i] ^ ].cap += now;
Cost += dis[T] * now;
Flow += now;
}
return Cost;
}
int main() {
freopen("a.in", "r", stdin);
int n = read(), tot = n;
if (n < ) { printf(""); return ; }
for (int i = ; i <= n; ++i) {
for (int j = ; j <= i; ++j) read();
for (int j = i + ; j <= n; ++j) {
int x = read();
id[i][j] = ++tot;
if (x == ) deg[j] ++, tag[tot] = ;
else if (x == ) deg[i] ++, tag[tot] = ;
else { add_edge(tot, i, , ); P[tot] = En; add_edge(tot, j, , ); }
}
}
S = , T = tot + ;
for (int i = n + ; i <= tot; ++i) add_edge(S, i, , );
for (int i = ; i <= n; ++i)
for (int j = deg[i]; j < n - ; ++j) add_edge(i, T, , j);
int ans = n * (n - ) * (n - ) / ;
for (int i = ; i <= n; ++i)
ans -= deg[i] * (deg[i] - ) / ;
ans -= mcf();
printf("%d\n", ans);
for (int i = ; i <= n; ++i)
for (int j = i + ; j <= n; ++j) {
if (tag[id[i][j]]) A[i][j] = tag[id[i][j]] - ;
else A[i][j] = e[P[id[i][j]]].cap ? : ;
}
for (int i = ; i <= n; ++i, puts("")) {
for (int j = ; j < i; ++j) printf("%d ", A[j][i] ^ );
for (int j = i; j <= n; ++j) printf("%d ", A[i][j]);
}
return ;
}

2597: [Wc2007]剪刀石头布的更多相关文章

  1. bzoj 2597 [Wc2007]剪刀石头布——费用流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2597 三个人之间的关系,除了“剪刀石头布”,就是有一个人赢了2局:所以考虑算补集,则每个人对 ...

  2. BZOJ.2597.[WC2007]剪刀石头布(费用流zkw)

    BZOJ 洛谷 \(Description\) 给定一张部分边方向已确定的竞赛图.你需要给剩下的边确定方向,使得图中的三元环数量最多. \(n\leq100\). \(Solution\) 这种选择之 ...

  3. bzoj 2597: [Wc2007]剪刀石头布【最小费用最大流】

    脑子不太清楚一个zz问题调了好久-- 首先正难则反,因为三元环好像没什么特点,就考虑让非三元环个数最小 考虑非三元环特点,就是环上一定有一个点的入度为2,联系整张图,三元环个数就是每个点C(入度,2) ...

  4. BZOJ 2597: [Wc2007]剪刀石头布(费用流)

    传送门 解题思路 考虑全集-不能构成三元环的个数.如果三个点不能构成三元环,一定有一个点的入度为\(2\),继续扩展,如果一个点的度数为\(3\),则会失去3个三元环.对于一个点来说,它所产生的不能构 ...

  5. [Wc2007]剪刀石头布[补集转化+拆边]

    2597: [Wc2007]剪刀石头布 Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 1157  Solved:  ...

  6. [Wc2007]剪刀石头布

    [Wc2007]剪刀石头布 http://www.lydsy.com/JudgeOnline/problem.php?id=2597 Time Limit: 20 Sec  Memory Limit: ...

  7. [bzoj2597][Wc2007]剪刀石头布_费用流

    [Wc2007]剪刀石头布 题目大意:https://www.lydsy.com/JudgeOnline/problem.php?id=2597 题解: 发现直接求三元环不好求,我们考虑任选三个点不是 ...

  8. 【BZOJ2597】[Wc2007]剪刀石头布 最小费用流

    [BZOJ2597][Wc2007]剪刀石头布 Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之 ...

  9. 【bzoj 2597】[Wc2007]剪刀石头布(图论--网络流 最小费用最大流)

    题目:在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道于统计有多少这样的 ...

随机推荐

  1. sql面试

    1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name   kecheng   fenshu 张三     语文       81张三     数学       75李四     语文   ...

  2. 一些centos 6和centos 7的区别

      CentOS 6(OLE 6,RHEL 6类似) CcnetOS 7(OLE 7,RHEL 7类似) 影响 默认文件系统 ext4 xfs 大量小文件在ext4上工作性能较好在64位linux中, ...

  3. iOS7中UIView的animateKeyframesWithDuration方法讲解

    iOS7中UIView的animateKeyframesWithDuration方法讲解 在iOS7中,给UIView添加了一个方法用来直接使用关键帧动画而不用借助CoreAnimation来实现,那 ...

  4. NSArray排序方法讲解

    NSArray排序方法讲解 给数组排序有着多种方式 最麻烦的是sortedArrayUsingSelector:,其次是sortedArrayUsingDescriptors:,最容易使用的就是sor ...

  5. [翻译] USING GIT IN XCODE [1] 在XCODE中使用GIT[1]

    USING GIT IN XCODE http://www.cimgf.com/2013/12/10/using-git-in-xcode/ Git has become a very popular ...

  6. Linux ifconfig命令详解

    ifconfig(interfaces config).通常需要以root身份登录或使用sudo来使用ifconfig工具 ifconfig 命令用来查看和配置网络设备.当网络环境发生改变时可通过此命 ...

  7. SparkSql实现Mysql到hive的数据流动

    今天去面试了一波,因为调度系统采用了SparkSql实现数据从Mysql到hive,在这一点上面试官很明显很不满我对于Spark的理解,19年的第一个面试就这么挂了. 有问题不怕,怕的是知道了问题还得 ...

  8. httpd:RSA certificate configured for SERVER does NOT include an ID which matches the server name

    这个是因为ssl认证丢失了密钥的问题,Apache的默认配置文件加载了mod_ssl模块,而且指定密钥对儿的位置,就是我测试salt-api时创建密钥对儿的位置.而且还有一个错误就是我密钥对儿指定的h ...

  9. 打包dll发布到nuget服务器

    几个月前上传过一次nuget包,结果好久不用,今天想更新下,完全忘记了怎么用了,又是一顿查,所以决定记录下来,当然这可能不是一个傻瓜式的教程,但聪明的你们应该能够看明白的,因为整体操作还是很简单的 好 ...

  10. linux-top命令查看内存CPU

    转自:https://www.cnblogs.com/dragonsuc/p/5512797.html 查看多核CPU命令 mpstat -P ALL  和  sar -P ALL 说明:sar -P ...