题目链接

题意

给出一个n*m的酒店,每个点是一个房间,要将这个酒店的房间划分成为两块(一块无烟区,一块吸烟区),相邻的两个房间之间有一条带权边,权值代表空气锁的面积,如果把这条边给去掉,那么需要花费(空气锁的面积+开一个窗口传食物)*1000元。问需要的最少花费是多少。要注意如果面积为0,则这条边不能划分

思路

全场做的人不多,主要看题意比较难,看懂题意就会发现是裸的最小割,但是有个面积为0的坑点。这里的边需要开的比较大,考虑到每次增加两个点,就会使边增加三条(不知道这样想对不对),于是就开两倍(还有两倍双向边)。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int M = 1e3 + 11;
const int N = 1e6 + 11;
struct Edge {
int u, v, nxt, cap;
} edge[N*4];
int head[N], tot, id[M][M];
int cur[N], gap[N], dis[N], pre[N];
queue<int> que; void Add(int u, int v, int cap) {
edge[tot] = (Edge) { u, v, head[u], cap }; head[u] = tot++;
edge[tot] = (Edge) { v, u, head[v], cap }; head[v] = tot++;
} void BFS(int T) {
while(!que.empty()) que.pop();
memset(dis, INF, sizeof(dis));
memset(gap, 0, sizeof(gap));
gap[0] = 1; dis[T] = 0;
que.push(T);
while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(dis[v] != INF) continue;
dis[v] = dis[u] + 1;
gap[dis[v]]++;
que.push(v);
}
}
} int ISAP(int S, int T, int n) {
BFS(T);
memcpy(cur, head, sizeof(cur));
int i, flow, u = pre[S] = S, index, ans = 0;
while(dis[S] < n) {
if(u == T) {
flow = INF, index = u;
for(u = S; u != T; u = edge[cur[u]].v)
if(edge[cur[u]].cap < flow) flow = edge[cur[u]].cap, index = u;
for(u = S; u != T; u = edge[cur[u]].v)
edge[cur[u]].cap -= flow, edge[cur[u]^1].cap += flow;
u = index, ans += flow;
}
for(i = cur[u]; ~i; i = edge[i].nxt)
if(dis[edge[i].v] == dis[u] - 1 && edge[i].cap) break;
if(~i) {
pre[edge[i].v] = u; cur[u] = i; u = edge[i].v;
} else {
if(--gap[dis[u]] == 0) break;
int md = n + 1;
for(i = head[u]; ~i; i = edge[i].nxt)
if(edge[i].cap && dis[edge[i].v] < md) md = dis[edge[i].v], cur[u] = i;
gap[dis[u] = md + 1]++;
u = pre[u];
}
} return ans;
} int main() {
int t; scanf("%d", &t);
while(t--) {
memset(head, -1, sizeof(head));
tot = 0;
int n, m, sx, sy, ex, ey;
scanf("%d%d", &n, &m);
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
int cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) id[i][j] = ++cnt;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m - 1; j++) {
int cap; scanf("%d", &cap);
if(cap) Add(id[i][j], id[i][j+1], cap + 1);
}
}
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < m; j++) {
int cap; scanf("%d", &cap);
if(cap) Add(id[i][j], id[i+1][j], cap + 1);
}
}
printf("%d\n", ISAP(id[sx][sy], id[ex][ey], n * m) * 1000);
} return 0;
}

Codeforces Gym101518H:No Smoking, Please(最小割)的更多相关文章

  1. Codeforces 724E Goods transportation(最小割转DP)

    [题目链接] http://codeforces.com/problemset/problem/724/E [题目大意] 每个城市有pi的物品可以运出去卖,si个物品可以买, 编号小的城市可以往编号大 ...

  2. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  3. Codeforces 786E. ALT 最小割+倍增

    E. ALT http://codeforces.com/problemset/problem/786/E 题意: 给出一棵 n 个节点的树与 m 个工人.每个工人有一条上下班路线(简单路径),一个工 ...

  4. Codeforces 628F 最大流转最小割

    感觉和昨天写了的题一模一样... 这种题也能用hall定理取check, 感觉更最小割差不多. #include<bits/stdc++.h> #define LL long long # ...

  5. Codeforces 1009G Allowed Letters 最大流转最小割 sosdp

    Allowed Letters 最直观的想法是贪心取, 然后网络流取check可不可行, 然后T了. 想到最大流可以等于最小割, 那么我们状压枚举字符代表的6个点连向汇点是否断掉, 然后再枚举64个本 ...

  6. Yet Another Maxflow Problem CodeForces - 903G (最小割,线段树)

    大意: 两个n元素集合$A$, $B$, $A_i$与$A_{i+1}$连一条有向边, $B_i$与$B_{i+1}$连一条有向边, 给定$m$条从$A_i$连向$B_j$的有向边, 每次询问修改$A ...

  7. CodeForces E. Goods transportation【最大流+dp最小割】

    妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...

  8. Codeforces 343E 最小割树

    题意及思路:https://www.cnblogs.com/Yuzao/p/8494024.html 最小割树的实现参考了这篇博客:https://www.cnblogs.com/coder-Uran ...

  9. 最小割 D. Behind the Wall Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest

    题目链接:http://codeforces.com/gym/101149/problem/D 题目大意: 堡垒受到攻击.堡垒是n*m的矩阵,矩阵里刚开始都是平地,然后那个数值表示在当前平地上建一面墙 ...

随机推荐

  1. ThreadPoolExecutor原理和使用

    大家先从ThreadPoolExecutor的整体流程入手: 针对ThreadPoolExecutor代码.我们来看下execute方法: public void execute(Runnable c ...

  2. Easyui Tab刷新

    Easyui Tab刷新: function refreshTab(title){ var tab = $('#id').tab('getTab',title); $('#id').tab('upda ...

  3. 【C/S通信交互之Http篇】Cocos2dx(Client)使用Curl与Jetty(Server)实现手机网游Http通信框架(内含解决curl.h头文件找不到问题)

    之前已经分享过一篇基于Cocos2dx与服务器使用Socket进行通信的框架,还不太熟悉的请移步到如下博文中: [C/S通信交互之Socket篇]Cocos2dx(Client)使用BSD Socke ...

  4. js div的显示和隐藏

    <head>    <title></title>    <style type="text/css">        div    ...

  5. 关于SetLength报Out of memory的研究及解决办法

    关于SetLength报Out of memory的研究及解决办法 最近在做一个GIS系统, 在读GIS数据时采用了动态数组,突然读一个数据时SetLength报错!Out of memory 仔细研 ...

  6. Codejock.Xtreme.Toolkit.Pro.v15.3.1 下载 与 VS2015补丁使用方法

    Codejock.Xtreme.Toolkit.Pro.v15.3.1 下载 与 VS2015补丁使用方法 打算放在CSDN进行下载的,上传完成后发现资源分设置的1分,本打算赚点下载分的.在页面上没有 ...

  7. linux自动挂载远程网盘到本地

    sudo vim /etc/fstab  添加如下内容 //192.168.1.110/MyFiles /path/to/mount cifs username=adminz,password=pas ...

  8. spring.net的简单使用(四)对象属性注入

    创建了对象,如果是简单对象就到此为止,如果是复杂对象,则需要为它的属性赋值. 属性赋值有两种方法:属性注入和构造器注入. 一.属性注入 在object节点下使用property就是属性注入,如下: & ...

  9. Windows Mount NFS Share from e.g. Linux

    Note: Not Stable, so steps below are for reference only ************ Linux Configuration NFS Share 1 ...

  10. QT5---应用程序发布(使用windeployqt和NSIS)

      采用动态编译的方式发布程序,即release版本. 找齐动态依赖库(.dll) 方法一   用Dependency Walker这个工具去找少了那些dll,不过这个工具也不怎么靠谱,一个比较靠谱但 ...