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 ...
随机推荐
- android 多线程 异步消息处理 服务 学习笔记 (六)
三种多线程编程方法 1 class Mythread extends Thread{ @Override public void run(){} } new Mythread().start() 2 ...
- ProjectServer任务审批后自动发布
我们知道ProjectServer汇报工时的顺序是这样: 1.项目成员打开自己的时间表,选择要汇报的任务,在汇报工时栏填写实际工时. 2.汇报工时后点击保存. 3.将汇报工时的任务提交给项目经理. 4 ...
- springboot+mybatisplus 测试代码生成
测试springboot + mybatisplus 实现代码生成 使用默认的模板引擎 pom.xml文件 <?xml version="1.0" encoding=&q ...
- 理解 ES6 Generator-next()方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vscode配置C++运行环境(2019/1//11更)并加入bits/stdc++.h头文件
因为重装系统,以前配置好的c++环境又没了.所以有要配置一遍. 1 下载mingw64或minw, 配置好环境变量:C://mingw64//bin; 在cmd下用g++ -v验证是否成功. 2. ...
- 解决ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)【亲测有效】
文件转自:https://blog.csdn.net/hua1011161696/article/details/80666025 问题:(MySQL 5.6社区版windows版) 忘记密码或其他一 ...
- MySQL多表数据查询(DQL)
数据准备: /* ------------------------------------创建班级表------------------------------------ */ CREATE TAB ...
- Angular : IOC的方式:依赖注入
依赖注入 @Component, @Injectable 可以允许别的声明在providers里面的Service等注入到被这两个装饰器装饰的类中 Service等可以被声明在app-module.t ...
- JZOJ 5943. 树
Description
- Spark-源码-Spark-Submit 任务提交
Spark 版本:1.3 调用shell, spark-submit.sh args[] 首先是进入 org.apache.spark.deploy.SparkSubmit 类中调用他的 main() ...