loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流
loj6068. 「2017 山东一轮集训 Day4」棋盘
链接
思路
上来没头绪,后来套算法,套了个网络流
经典二分图
左边横,右边列
先重新算一下行和列,就是他们x相通的的算一个
然后就去掉了障碍的作用
然后每一行贡献是递增的(0,1,2,3,4………)
直接暴力连上每条可能有的流量为1的边就行了
下面的图可能没啥用就是个普通二分图

错误
有的数组开小了
有的memset(1e6)
T成40
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 57, M = 3e5 + 7, inf = 0x3f3f3f3f;
int read() {
int x = 0, f = 1;
char s = getchar();
for (; s > '9' || s < '0'; s = getchar())if (s == '-')f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
int n, m, S, T, dsr[N * N];
char s[N][N];
struct node {
int u, v, nxt, cost, flow;
} e[M];
int head[M], tot = 1;
void add_edge(int u, int v, int flow, int cost) {
e[++tot].v = v, e[tot].u = u;
e[tot].cost = cost, e[tot].flow = flow, e[tot].nxt = head[u], head[u] = tot;
}
void add(int u, int v, int flow, int cost) {
add_edge(u, v, flow, cost);
add_edge(v, u, 0, -cost);
}
int dis[M], vis[M], frm[M];
int spfa() {
for (int i = 1; i <= T; ++i) dis[i] = inf, vis[i] = 0;
queue<int> q;
q.push(S);
dis[S] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (dis[u] + e[i].cost < dis[v] && e[i].flow > 0) {
dis[v] = dis[u] + e[i].cost;
frm[v] = i;
if (!vis[v]) {
q.push(v);
vis[v] = 1;
}
}
}
}
return dis[T] != inf;
}
int ans, maxflow;
void work() {
spfa();
int mn = inf;
for (int i = T; i != S; i = e[frm[i]].u) mn = min(e[frm[i]].flow, mn);
for (int i = T; i != S; i = e[frm[i]].u) {
e[frm[i]].flow -= mn;
e[frm[i] ^ 1].flow += mn;
ans += e[frm[i]].cost * mn;
}
maxflow += mn;
}
int belong_x[N][N], belong_y[N][N], hav_x[N * N], hav_y[N * N], num_x, num_y;
int main() {
n = read();
for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
int js = n * n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (s[i][j] == '#') {
js--;
continue;
}
belong_x[i][j] = (s[i][j - 1] == '.') ? belong_x[i][j - 1] : ++num_x;
belong_y[i][j] = (s[i - 1][j] == '.') ? belong_y[i - 1][j] : ++num_y;
hav_x[belong_x[i][j]]++;
hav_y[belong_y[i][j]]++;
}
}
S = num_x + num_y + 1, T = num_x + num_y + 2;
for (int i = 1; i <= num_x; ++i)
for (int j = 0; j < hav_x[i]; ++j) add(S, i, 1, j);
for (int i = 1; i <= num_y; ++i)
for (int j = 0; j < hav_y[i]; ++j) add(i + num_x, T, 1, j);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (s[i][j] == '.')
add(belong_x[i][j], num_x + belong_y[i][j], 1, 0);
for (int i = 1; i <= js; ++i) work(), dsr[i] = ans;
m = read();
while (m--) {
int x = read();
if (x > js)
puts("0");
else
printf("%d\n", dsr[x]);
}
return 0;
}
loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流的更多相关文章
- Loj 6068. 「2017 山东一轮集训 Day4」棋盘
Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...
- 「2017 山东一轮集训 Day4」棋盘(费用流)
棋盘模型 + 动态加边 #include<cstdio> #include<algorithm> #include<iostream> #include<cs ...
- [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]
题意 题目链接 分析 考虑每个棋子对对应的横向纵向的极大区间的影响:记之前这个区间中的点数为 \(x\) ,那么此次多配对的数量即 \(x\) . 考虑费用流,\(S\rightarrow 横向区间 ...
- LOJ 6068「2017 山东一轮集训 Day4」棋盘
题意 一个 \(n\times n\) 的棋盘上面有若干障碍物. 定义两个棋子可以互相攻击当且仅当这两个棋子的横坐标或纵坐标相等而且中间不能隔着障碍物.(可以隔棋子) 有 \(q\) 次询问,每次询问 ...
- Loj #6069. 「2017 山东一轮集训 Day4」塔
Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...
- 「2017 山东一轮集训 Day4」基因
设置 \(\sqrt{n}\) 个关键点,维护出关键点到每个右端点之间的答案以及Pam的左指针,每次暴力向左插入元素即可,为了去重,还需要记录一下Pam上每个节点在每个关键点为左端点插入到时候到最左边 ...
- Loj #6073.「2017 山东一轮集训 Day5」距离
Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...
- 「2017 山东一轮集训 Day5」苹果树
「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...
- 【LOJ#6066】「2017 山东一轮集训 Day3」第二题(哈希,二分)
[LOJ#6066]「2017 山东一轮集训 Day3」第二题(哈希,二分) 题面 LOJ 题解 要哈希是很显然的,那么就考虑哈希什么... 要找一个东西可以表示一棵树,所以我们找到了括号序列. 那么 ...
随机推荐
- js 读秒
<input type="button" value=" 获取验证码 " class="verification right" &qu ...
- PL/SQL控制结构
顺序结构 按先后顺序 分支判断结构 IF语句 IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statem ...
- 13.vue组件
vue组件(一) 组件嵌套: 1.全局嵌套: <!doctype html> <html> <head> <meta charset="utf-8& ...
- 2018秋寒假作业4—PTA编程总结1
7-1 打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 所谓"沙漏形状",是指每行输出奇数个符 ...
- [No0000199]设计模式总结
设计模式之间的关系: 设计模式总概况: 一.设计原则 .单一职责原则 一个类,只有一个引起它变化的原因.应该只有一个职责.每一个职责都是变化的一个轴线,如果一个类有一个以上的职责,这些职责就耦合在了一 ...
- Web开发——HTML基础(HTML响应式Web设计 Bootstrap)
参考: 参考:http://www.bootcss.com/ 目录: 1.什么是响应式 Web 设计? 2.创建自己的响应设计 3.使用 Bootstrap 1.什么是响应式 Web 设计? RWD ...
- Java ee第五周作业
Applet: Applet是采用Java编程语言编写的小应用程序,该程序可以包含在 HTML(标准通用标记语言的一个应用)页中,与在页中包含图像的方式大致相同. 含有Applet的网页的HTML文件 ...
- Jedis连接 HelloWorld实现
建一个Maven项目, pom里加下jedis依赖, <dependency> <groupId>redis.clients</groupId> <artif ...
- iot-web增加apis-namespace组件
1 文件夹复制 apis 2 增加 3 增加module
- 取数游戏II
传送门 #include <bits/stdc++.h> using namespace std; #define ll long long #define re register #de ...