BZOJ 2597 剪刀石头布(最小费用最大流)(WC2007)
Description
Input
Output
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXV = MAXN * MAXN;
const int MAXE = MAXN * MAXV;
const int INF = 0x7f7f7f7f; struct ZWK_FLOW {
int head[MAXV], dis[MAXV];
int to[MAXE], next[MAXE], flow[MAXE], cost[MAXE];
int n, ecnt, st, ed; void init() {
memset(head, , sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c, int w) {
to[ecnt] = v; flow[ecnt] = c; cost[ecnt] = w; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; flow[ecnt] = ; cost[ecnt] = -w; next[ecnt] = head[v]; head[v] = ecnt++;
//printf("%d %d %d %d\n", u, v, c, w);
} void spfa() {
for(int i = ; i <= n; ++i) dis[i] = INF;
priority_queue<pair<int, int> > que;
dis[st] = ; que.push(make_pair(, st));
while(!que.empty()) {
int u = que.top().second, d = -que.top().first; que.pop();
if(d != dis[u]) continue;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && dis[v] > d + cost[p]) {
dis[v] = d + cost[p];
que.push(make_pair(-dis[v], v));
}
}
}
int t = dis[ed];
for(int i = ; i <= n; ++i) dis[i] = t - dis[i];
} int minCost, maxFlow;
bool vis[MAXV]; int add_flow(int u, int aug) {
if(u == ed) {
maxFlow += aug;
minCost += dis[st] * aug;
return aug;
}
vis[u] = true;
int now = aug;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && !vis[v] && dis[u] == dis[v] + cost[p]) {
int t = add_flow(v, min(now, flow[p]));
flow[p] -= t;
flow[p ^ ] += t;
now -= t;
if(!now) break;
}
}
return aug - now;
} bool modify_label() {
int d = INF;
for(int u = ; u <= n; ++u) if(vis[u]) {
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(flow[p] && !vis[v]) d = min(d, dis[v] + cost[p] - dis[u]);
}
}
if(d == INF) return false;
for(int i = ; i <= n; ++i) if(vis[i]) dis[i] += d;
return true;
} int min_cost_flow(int ss, int tt, int nn) {
st = ss, ed = tt, n = nn;
minCost = maxFlow = ;
spfa();
while(true) {
while(true) {
for(int i = ; i <= n; ++i) vis[i] = false;
if(!add_flow(st, INF)) break;
}
if(!modify_label()) break;
}
return minCost;
}
} G; int n, m;
int mat[MAXN][MAXN], ans[MAXN][MAXN]; inline int encode(int i, int j) {
if(i > j) swap(i, j);
return i * n + j;
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; ++i) for(int j = ; j <= n; ++j) scanf("%d", &mat[i][j]);
m = n * n;
int ss = n + m + , tt = ss + ;
G.init();
int sum = n * (n - ) * (n - ) / ;
for(int i = ; i <= n; ++i) {
for(int j = , tmp = ; j < n; ++j, tmp += ) G.add_edge(ss, i, , tmp);
for(int j = ; j <= n; ++j) if(mat[i][j] != )
ans[i][j] = G.ecnt, G.add_edge(i, encode(i, j), , );
}
for(int i = ; i <= m; ++i) G.add_edge(i + n, tt, , );
int x = G.min_cost_flow(ss, tt, tt);
printf("%d\n", sum - (x - n * (n - ) / ) / );
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) {
if(j != ) printf(" ");
if(mat[i][j] != ) printf("%d", mat[i][j]);
else {
if(G.flow[ans[i][j]] == ) printf("");
else printf("");
}
}
puts("");
}
}
BZOJ 2597 剪刀石头布(最小费用最大流)(WC2007)的更多相关文章
- bzoj 2597 剪刀石头布 —— 拆边费用流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2597 不合法的三个人之间的关系就是一个人赢了两次: 记 \( deg[i] \) 表示第 \ ...
- 【BZOJ-2597】剪刀石头布 最小费用最大流
2597: [Wc2007]剪刀石头布 Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1016 Solved: ...
- BZOJ2597 [Wc2007]剪刀石头布(最小费用最大流)
题目大概是说n个人两两进行比赛,问如何安排几场比赛的输赢使得A胜B,B胜C,C胜A这种剪刀石头布的三元组最多. 这题好神. 首先,三元组总共有$C_n^3$个 然后考虑最小化不满足剪刀石头布条件的三元 ...
- BZOJ 2668 [cqoi2012]交换棋子 | 最小费用最大流
传送门 BZOJ 2668 题解 同时分别限制流入和流出次数,所以把一个点拆成三个:入点in(x).中间点mi(x).出点ou(x). 如果一个格子x在初始状态是黑点,则连(S, mi(x), 1, ...
- BZOJ 3876 [AHOI/JSOI2014]支线剧情 (最小费用可行流)
题面:洛谷传送门 BZOJ传送门 题目大意:给你一张有向无环图,边有边权,让我们用任意条从1号点开始的路径覆盖这张图,需要保证覆盖完成后图内所有边都被覆盖至少一次,求覆盖路径总长度的最小值 最小费用可 ...
- 【BZOJ】1221: [HNOI2001] 软件开发(最小费用最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1221 先吐槽一下,数组依旧开小了RE:在spfa中用了memset和<queue>的版本 ...
- BZOJ 1927 星际竞速(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1927 题意:一个图,n个点.对于给出的每条边 u,v,w,表示u和v中编号小的那个到编号 ...
- BZOJ 1061 志愿者招募(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1061 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管.布 ...
- bzoj 1877 [SDOI2009]晨跑(最小费用最大流)
Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十 ...
- bzoj 1927 [Sdoi2010]星际竞速(最小费用最大流)
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1576 Solved: 954[Submit][Statu ...
随机推荐
- c语言描述的顺序表实现
//顺序表的实现:(分配一段连续地址给顺序表,像数组一样去操作) #include<stdio.h> #include<stdlib.h> #define OK 1 #defi ...
- LeetCode8.字符串转换整数(atoi) JavaScript
请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...
- php第四节(循环和函数)
<?php //循环有 for(){}.有while(){} 有do{}while().foreach(){}循环 //1.for(){} for($i=0;$i<=9;$i++){ ec ...
- jzoj100029. 【NOIP2017提高A组模拟7.8】陪审团(贪心,排序)
Description 陪审团制度历来是司法研究中的一个热议话题,由于陪审团的成员组成会对案件最终的结果产生巨大的影响,诉讼双方往往围绕陪审团由哪些人组成这一议题激烈争夺. 小 W 提出了一个甲乙双方 ...
- node-zookeeper-dubbo 和egg实现远程连接
基于js的node-zookeeper-dubbo 和egg实现远程连接服务 const nzd = require('node-zookeeper-dubbo'); const opt={ appl ...
- 【linux基于Postfix和Dovecot邮件系统的搭建】
一:PostFixe和Dovecot的简单介绍 Postfix postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.postfix是Wietse Ven ...
- JavaScript鼠标事件
mousedown 鼠标被按下. mouseup 鼠标按钮被释放(抬起). click 鼠标左键(或中建)被单击. 事件触发顺序:mousedown>mouseup>click>db ...
- PrestaShop 网站漏洞修复如何修复
PrestaShop网站的漏洞越来越多,该网站系统是很多外贸网站在使用的一个开源系统,从之前的1.0初始版本到现在的1.7版本,经历了多次的升级,系统使用的人也越来越多,国内使用该系统的外贸公司也很多 ...
- ansible自动化运维入门
1.ansible的安装 1)使用源码安装Python3.5 安装支持包 yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses- ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...