题解 marshland
是个最大费用可行流
这题的建边很毒瘤
首先有危险度的点肯定要拆点
关键在于其它点怎么办
如果拆了不好保证每个点只经过一次
不拆连网络流都跑不了
但仔细观察题面,不能不难(???)发现一个L中那两个坐标和为偶数的点一定分两种
(奇, 奇)和(偶, 偶)
那可以用这个性质建边,一类连源点,一类连汇点就行了
- 所以总结是啥?坐标题善于发现坐标的奇偶性规律?
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 5000
#define ll long long
#define fir first
#define sec second
#define make make_pair
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, m, k;
int head[N], size=1, val[55][55], tot, s1, s2, t, dis[N], inc[N], back[N], ans, sum;
bool lim[55][55], vis[N];
pair<int, int> mp[55][55];
struct edge{int to, next, flw, cst;}e[N<<3];
inline void add(int s, int t, int f, int c) {e[++size].to=t; e[size].flw=f; e[size].cst=c; e[size].next=head[s]; head[s]=size;}
bool spfa(int s, int t) {
//cout<<"spfa "<<s<<' '<<t<<endl;
memset(dis, 127, sizeof(dis));
memset(back, -1, sizeof(back));
dis[s]=0; inc[s]=INF;
queue<int> q;
q.push(s);
int u;
while (q.size()) {
u=q.front(); q.pop();
vis[u]=0;
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (e[i].flw && dis[v]>dis[u]+e[i].cst) {
dis[v]=dis[u]+e[i].cst;
//cout<<"dis: "<<dis[v]<<endl;
back[v]=i; inc[v]=min(inc[u], e[i].flw);
if (!vis[v]) q.push(v), vis[v]=1;
}
}
}
return ~back[t];
}
signed main()
{
memset(head, -1, sizeof(head));
n=read(); m=read(); k=read();
for (int i=1; i<=n; ++i)
for (int j=1; j<=n; ++j)
val[i][j]=read(), sum+=val[i][j]; //, cout<<val[i][j]<<endl;
for (int i=1,x,y; i<=k; ++i) {
x=read(); y=read();
lim[x][y]=1;
}
for (int i=1; i<=n; ++i)
for (int j=1; j<=n; ++j)
if (!lim[i][j]) {
if ((i+j)&1) mp[i][j]=make(tot+1, tot+2), tot+=2;
else mp[i][j]=make(++tot, 0);
}
s1=++tot, s2=++tot, t=++tot;
//cout<<"s: "<<s1<<' '<<s2<<' '<<t<<endl;
for (int i=1; i<=n; ++i)
for (int j=1; j<=n; ++j) if (!lim[i][j]) {
if ((i+j)&1) {
add(mp[i][j].fir, mp[i][j].sec, 1, -val[i][j]), add(mp[i][j].sec, mp[i][j].fir, 0, val[i][j]);
}
else {
if (i&1) {
add(s2, mp[i][j].fir, 1, 0), add(mp[i][j].fir, s2, 0, 0);
if (i<n && !lim[i+1][j]) add(mp[i][j].fir, mp[i+1][j].fir, 1, 0), add(mp[i+1][j].fir, mp[i][j].fir, 0, 0);
if (j<n && !lim[i][j+1]) add(mp[i][j].fir, mp[i][j+1].fir, 1, 0), add(mp[i][j+1].fir, mp[i][j].fir, 0, 0);
if (i>1 && !lim[i-1][j]) add(mp[i][j].fir, mp[i-1][j].fir, 1, 0), add(mp[i-1][j].fir, mp[i][j].fir, 0, 0);
if (j>1 && !lim[i][j-1]) add(mp[i][j].fir, mp[i][j-1].fir, 1, 0), add(mp[i][j-1].fir, mp[i][j].fir, 0, 0);
}
else {
add(mp[i][j].fir, t, 1, 0), add(t, mp[i][j].fir, 0, 0);
if (i>1 && !lim[i-1][j]) add(mp[i-1][j].sec, mp[i][j].fir, 1, 0), add(mp[i][j].fir, mp[i-1][j].sec, 0, 0);
if (j>1 && !lim[i][j-1]) add(mp[i][j-1].sec, mp[i][j].fir, 1, 0), add(mp[i][j].fir, mp[i][j-1].sec, 0, 0);
if (i<n && !lim[i+1][j]) add(mp[i+1][j].sec, mp[i][j].fir, 1, 0), add(mp[i][j].fir, mp[i+1][j].sec, 0, 0);
if (j<n && !lim[i][j+1]) add(mp[i][j+1].sec, mp[i][j].fir, 1, 0), add(mp[i][j].fir, mp[i][j+1].sec, 0, 0);
}
}
}
add(s1, s2, 0, 0), add(s2, s1, 0, 0);
//cout<<"pos1"<<endl;
int tem=0;
for (int i=1,it=size-1; i<=m; ++i) {
++e[it].flw;
while (spfa(s1, t)) {
tem+=dis[t];
ans=min(ans, tem);
//cout<<"ans: "<<ans<<endl;
for (int u=t; u!=s1; u=e[back[u]^1].to) {
e[back[u]].flw-=inc[t];
e[back[u]^1].flw+=inc[t];
}
}
}
printf("%d\n", sum+ans);
return 0;
}
题解 marshland的更多相关文章
- 2018HN省队集训
HNOI2018省队集训 Day 1 流水账 T1 tree 换根+求\(lca\)+求子树和,一脸bzoj3083遥远的国度的既视感.子树和讨论一下就好了,\(lca\)?也是大力讨论一波. 先写了 ...
- [2018HN省队集训D5T1] 沼泽地marshland
[2018HN省队集训D5T1] 沼泽地marshland 题意 给定一张 \(n\times n\) 的棋盘, 对于位置 \((x,y)\), 若 \(x+y\) 为奇数则可能有一个正权值. 你可以 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
随机推荐
- centos安装报错:license information (license not accepted)
前言:在最近部署的centos系统发现个问题 出现报错:安装配置完成后,重启虚拟机出现license information (license not accepted) 截图: 解决方案: 在界 ...
- PYTHON2.7安装 pyinstaller出错,不能正常安装
解决方法: pip2.7 install pyinstaller==3.4
- Hive——连接方式
Hive--连接方式 一.CLI连接 直接通过CLI连接hive,进行相关hive sql 操作. 直接使用 hive-1.1.0-cdh5.7.0/bin/hive 命令即可 hive> ...
- python + pytest 基本使用方法(Fixture)
#firtures通常用来对测试方法.测试函数.测试类和整个测试文件进行初始化或还原测试环境# setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行 ...
- request - cookie 操作(一)
from urllib import request#headers 带cookieblog_url = "http://www.renren.com/452057374/profile?r ...
- POJ3615-Floyd
http://poj.org/problem?id=3615 因为只需要求所在路径的最大高度的最小值,而且n<=300,我们可以用floyd跑. g[i][j]=min(g[i][j],max( ...
- 【Uva11400 Lighting System Design】动态规划
分析 先按照电压从小到大排序,做一下前缀和s[i]求i之前的电灯泡的数量. 状态:$ F_i\(表示到\) i$个灯泡的最小开销. 状态转移方程:$ F_i=F_j+(s[i]-s[j])\times ...
- nmcli命令行修改网络连接名称
目前在网上能找到的文章中,使用nmcli命令修改Linux系统中网卡连接的名称都是先创建新的连接,然后删除旧的连接的方式 此种方式其实完全不恰当,简直就是在浪费时间,nmcli命令本身就提供了直接修改 ...
- input输入框只能输入正数和小数(保留小数点后两位)
1.限制只能输入正数和小数保留小数点后两位 1 <input type="number" id="txtNum" /> 2 3 <script ...
- icmp介绍以及arp攻击
目录 一.ip数据包格式 二.ICMP协议介绍 三.ARP协议介绍 四.ARP攻击原理 一.ip数据包格式 网络层的功能: 定义了基于ip协议的逻辑地址 连接不同的媒介类型 选择是数据通过网络的最佳途 ...