[bzoj2597][Wc2007]剪刀石头布_费用流
[Wc2007]剪刀石头布
题目大意:https://www.lydsy.com/JudgeOnline/problem.php?id=2597
题解:
发现直接求三元环不好求,我们考虑任选三个点不是三元环的个数。
这样的话,必定是有一个点被其余两个点指,我们就根据这个来求。
又发现,最后的答案之和所有点的度数有关。
就是,$\sum C_{d_i}^{2}$。
紧接着,因为度数和是一定的。而且已经有了一些边。
现在就是有固定的度数可以分配,每个点有一个分配上限,怎么分配最少?
发现一个事,就是$C_{d_i + 1}^{2} - C_{d_i} ^ {2} < C_{d_i + 2}^{2} - C_{d_i + 1}^{2}$。
根据这个性质,我们就可以暴力连边费用流了。
我们就把权值的差当做费用,它肯定会从低往高走因为是最小费用,满足题意。
代码:
#include <bits/stdc++.h>
#define N 11010
#define M 500010
using namespace std;
int head[N], to[M], pre[N], nxt[M], val[M], cst[M], tot = 1;
int S = N - 1, T = N - 2, dis[N];
bool vis[N];
queue <int> q;
char *p1, *p2, buf[100000];
#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )
int rd() {
int x = 0;
char c = nc();
while (c < 48) {
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x;
}
inline void add2(int x, int y, int z, int w) {
to[ ++ tot] = y;
nxt[tot] = head[x];
val[tot] = z;
cst[tot] = w;
head[x] = tot;
}
inline void add(int x, int y, int z, int w) {
add2(x, y, z, w);
add2(y, x, 0, -w);
}
bool spfa() {
while (!q.empty()) {
q.pop();
}
memset(pre, 0, sizeof pre);
memset(dis, 0x3f, sizeof dis);
dis[S] = 0;
q.push(S);
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = false;
for (int i = head[x]; i; i = nxt[i]) {
if (dis[to[i]] > dis[x] + cst[i] && val[i]) {
dis[to[i]] = dis[x] + cst[i];
pre[to[i]] = i ^ 1;
if (!vis[to[i]]) {
vis[to[i]] = true;
q.push(to[i]);
}
}
}
}
return pre[T];
}
int mincost() {
int re = 0;
while (spfa()) {
int mdl = 0x3f3f3f3f;
for (int i = T; i != S; i = to[pre[i]]) {
mdl = min(mdl, val[pre[i] ^ 1]);
}
for (int i = T; i != S; i = to[pre[i]]) {
val[pre[i] ^ 1] -= mdl;
val[pre[i]] += mdl;
re += mdl * cst[pre[i] ^ 1];
}
}
return re;
}
int A[110][110], id[110][110];
int main() {
int n = rd();
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= n; j ++ ) {
A[i][j] = rd();
}
}
int cnt = n;
for (int i = 1; i <= n; i ++ ) {
for (int j = i + 1; j <= n; j ++ ) {
if (A[i][j] == 2) {
cnt ++ ;
add(cnt, i, 1, 0);
id[i][j] = tot - 1;
add(cnt, j, 1, 0);
id[j][i] = tot - 1;
add(S, cnt, 1, 0);
}
}
}
// cout << tot << ' ' << cnt << endl ;
for (int i = 1; i <= n; i ++ ) {
int c = 0;
for (int j = 1; j <= n; j ++ ) {
if (A[i][j] == 1) {
c ++ ;
}
}
if (c) {
add(S, i, c, 0);
}
}
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= n; j ++ ) {
add(i, T, 1, j * (j - 1) / 2 - (j - 1) * (j - 2) / 2);
}
}
int ans = mincost();
ans = n * (n - 1) * (n - 2) / 6 - ans;
cout << ans << endl ;
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= n; j ++ ) {
if (A[i][j] != 2) {
printf("%d ", A[i][j]);
}
else {
if (i == j) {
printf("0 ");
}
else {
if (!val[id[i][j]]) {
printf("1 ");
}
else {
printf("0 ");
}
}
}
}
puts("");
}
return 0;
}
/*
6
0 2 1 0 2 1
2 0 0 2 2 2
0 1 0 2 1 1
1 2 2 0 1 2
2 2 0 0 0 2
0 2 0 2 2 0
*/
小结:这个费用流很巧妙。就是有先后的选取过程,但是保证后面的代价比前面的代价大,我们可以暴力建边。
[bzoj2597][Wc2007]剪刀石头布_费用流的更多相关文章
- BZOJ2597 [Wc2007]剪刀石头布 【费用流】
题目链接 BZOJ2597 题解 orz思维差 既然是一张竞赛图,我们选出任意三个点都可能成环 总方案数为 \[{n \choose 3}\] 如果三个点不成环,会发现它们的度数是确定的,入度分别为\ ...
- bzoj2597: [Wc2007]剪刀石头布(费用流)
传送门 不得不说这思路真是太妙了 考虑能构成三元组很难,那我们考虑不能构成三元组的情况是怎么样 就是说一个三元组$(a,b,c)$,其中$a$赢两场,$b$赢一场,$c$没有赢 所以如果第$i$个人赢 ...
- BZOJ2597 WC2007剪刀石头布(费用流)
考虑使非剪刀石头布情况尽量少.设第i个人赢了xi场,那么以i作为赢家的非剪刀石头布情况就为xi(xi-1)/2种.那么使Σxi(xi-1)/2尽量小即可. 考虑网络流.将比赛建成一排点,人建成一排点, ...
- BZOJ_2661_[BeiJing wc2012]连连看_费用流
BZOJ_2661_[BeiJing wc2012]连连看_费用流 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规 ...
- BZOJ_1834_[ZJOI2010]network 网络扩容_费用流
BZOJ_1834_[ZJOI2010]network 网络扩容_费用流 题意: 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求: 1.在不扩容的 ...
- BZOJ_1877_[SDOI2009]晨跑_费用流
BZOJ_1877_[SDOI2009]晨跑_费用流 题意: Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他 坚持下来的只有晨跑. 现在给出 ...
- BZOJ_1070_[SCOI2007]修车_费用流
BZOJ_1070_[SCOI2007]修车_费用流 Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同 的车进行维修所用的时间 ...
- 【BZOJ-2597】剪刀石头布 最小费用最大流
2597: [Wc2007]剪刀石头布 Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1016 Solved: ...
- [WC2007]剪刀石头布(最大流)
洛古 一句话题意:给定一张图,每两点之间有一条有向边或无向边,把所有无向边定向,使图中三元环个数尽量多 因为原图是一个完全图,假设图中任意三点都能构成三元环,那么途中三元环的个数为:\(\binom{ ...
随机推荐
- toggleClass(class|fn[,sw])
toggleClass(class|fn[,sw]) 概述 如果存在(不存在)就删除(添加)一个类.直线电机参数 参数 classStringV1.0 CSS类名 class,switchStri ...
- xml------文件打开样式
-----添加css样式修饰 引入css样式 浏览器展示 -------- 在服务器上通过 XSLT 转换 XML xsl文件 样式展示
- BZOJ 1706: [usaco2007 Nov]relays 奶牛接力跑 倍增Floyd
题不难,但是一开始把读入看错了,调了半天qaq~ Code: #include <bits/stdc++.h> #define N 300 #define setIO(s) freopen ...
- 【线性代数】2-6:三角矩阵( $A=LU$ and $A=LDU$ )
title: [线性代数]2-6:三角矩阵( A=LUA=LUA=LU and A=LDUA=LDUA=LDU ) toc: true categories: Mathematic Linear Al ...
- JS学习-01
01
- 数据结构实验之链表八:Farey序列(SDUT 3331)
#include <bits/stdc++.h> using namespace std; typedef struct node { int data2; int data1;//mu ...
- 浅谈C++运算符重载
首先,什么是运算符重载? C++里运算符重载主要有两种方法.一是通过类的成员函数进行重载,二是通过类的友元函数进行重载. 下面以简单的复数类complex为例: 下面是通过类的成员函数进行运算符的重载 ...
- Leetcode题目155.最小栈(简单)
题目描述: 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中.pop() -- 删除栈顶的元素.top() -- 获取栈顶 ...
- vue-cli3.0 初体验
vue-cli3.0 自我记录 其实在2018年8月10号,vue-cli3.0就已经面世了,由于项目中应用的全是2.x版本,所以并不了解3.0的vue-cli发生了什么变化,那今天尝试了下遇见的问题 ...
- shiro中接入单点登录功能
最近新建的系统中使用了shiro,而shiro框架中包含登录认证和鉴权的功能,因为我们系统要统一接入公司内部的单点登录(isso)系统,所以通过isso的登录用户,需要在shiro中置为已认证,一下提 ...