有上下界的费用流

#include <stdio.h>
#include <algorithm>
#include <queue>
#include <cstring> // begin{最小费用最大流}
struct edge {int from, to, cap, flow, cost, next;}; const int MAXN = 50 + 7;
const int inf = 0x3f3f3f3f; edge e[MAXN * MAXN + 7];
int head[MAXN * 2 + 7];
int e_sz, supS, supT, sum_l; inline void add_edge(int from, int to, int cap, int cost) {
if(!cap) return;
// printf("%d %d %d %d\n", from, to, cap, cost);
e[e_sz].from = from, e[e_sz].to = to, e[e_sz].cap = cap, e[e_sz].cost = cost, e[e_sz].flow = 0, e[e_sz].next = head[from]; head[from] = e_sz++;
e[e_sz].from = to, e[e_sz].to = from, e[e_sz].cap = 0, e[e_sz].cost = -cost, e[e_sz].flow = 0, e[e_sz].next = head[to]; head[to] = e_sz++;
} inline void add_edge_with_s(int u, int v, int l, int h, int c) {
//printf("edge_with_strict %d %d %d %d %d\n", u, v, l, h, c);
sum_l += l; add_edge(supS, v, l, c); add_edge(u, supT, l, c); add_edge(u, v, h - l, c);
} std::queue<int>q;
int dis[MAXN * 2], pre[MAXN * 2]; bool vis[MAXN * 2];
bool spfa(int s, int t) {
for(int i = 0; i < MAXN * 2; ++i)dis[i] = inf, vis[i] = false, pre[i] = -1;
// printf("%d %d\n", s, t);
dis[s] = 0, vis[s] = true; q.push(s);
while(!q.empty()) {
int u = q.front(); q.pop(); vis[u] = false;
for(int i = head[u], v; i != -1; i = e[i].next) {
v = e[i].to;
if(e[i].cap > e[i].flow && dis[v] > dis[u] + e[i].cost) {
dis[v] = dis[u] + e[i].cost, pre[v] = i;
if(!vis[v]) vis[v] = true, q.push(v);
}
}
}
return (pre[t] != -1);
} //返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost) {
int flow = 0, Min; cost = 0;
while(spfa(s, t)) {
Min = inf;
for(int i = pre[t]; i != -1; i = pre[e[i ^ 1].to])
if(Min > e[i].cap - e[i].flow) Min = e[i].cap - e[i].flow;
for(int i = pre[t]; i != -1; i = pre[e[i ^ 1].to])
e[i].flow += Min, e[i ^ 1].flow -= Min, cost += e[i].cost * Min;
flow += Min;
}
return flow;
}
// end{最小费用最大流} int mat[MAXN][MAXN], Rl[MAXN], Rh[MAXN], Cl[MAXN], Ch[MAXN], cntR[MAXN], cntC[MAXN];
int main() {
int n,s,t;
while(~scanf("%d", &n)) {
memset(head, -1, sizeof(head)); e_sz = 0;
memset(cntR, 0, sizeof(cntR)); memset(cntC, 0, sizeof(cntC));
s = n * 2 + 1, t = n * 2 + 2;
supS = n * 2 + 3, supT = n * 2 + 4, sum_l = 0;
for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j)scanf("%d", &mat[i][j]);
for(int i = 0; i < n; ++i) scanf("%d%d", Rl + i, Rh + i);
for(int i = 0; i < n; ++i) scanf("%d%d", Cl + i, Ch + i); for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; j++) cntR[i] += mat[i][j], cntC[i] += mat[j][i];
add_edge_with_s(s, i + 1, cntR[i], cntR[i], 0), add_edge_with_s(s, i + 1 + n, cntC[i], cntC[i], 0);
//add_edge(s, i + 1, cntR[i], 0), add_edge(s, i + 1 + n, cntC[i], 0);
add_edge_with_s(i + 1, t, Rl[i], Rh[i], 0), add_edge_with_s(i + n + 1, t, Cl[i], Ch[i], 0);
}
add_edge(t, s, inf, 0);
for(int i = 0, x1, x2, y1, y2; i < n * n / 2; ++i) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x1--, y1--, x2--, y2--;
if(mat[x1][y1] == mat[x2][y2])continue;
if(!mat[x1][y1]) std::swap(x1, x2), std::swap(y1, y2);
if(x1 == x2) add_edge(y1 + 1 + n, y2 + 1 + n, 1, 1);
else if(y1 == y2) add_edge(x1 + 1, x2 + 1, 1, 1);
}
//printf("input done\n");
int cost, flow = minCostMaxflow(supS, supT, cost);
// printf("total flow = %d\n", flow);
if(flow != sum_l) puts("-1");
else printf("%d\n", cost);
}
return 0;
}

[HihoCoder-1424] Asa's Chess Problem的更多相关文章

  1. 【hihocoder 1424】 Asa's Chess Problem(有源汇上下界网络流)

    UVALive-7670 ICPC北京2016-C题 hihocoder 1424 题意 有个 \(N\times N\) 的棋盘,告诉你每个格子黑色(1)或白色(0),以及每对能相互交换的同行或同列 ...

  2. Asa's Chess Problem

    一.题目 给定一张 \(n\times n\) 的矩阵,每个点上面有黑棋或者是白棋,给定 \(\frac{n\times n}{2}\) 对可以交换的位置,每对位置一定在同一行 \(/\) 同一列.\ ...

  3. UVa 750 - 8 Queens Chess Problem

    题目大意:八皇后问题,在一个8*8的棋盘上,放置8个皇后,使得任意两个皇后不在同一行上.不在同一列上.不在同一条对角线上,不过这道题预先给定了一个位置放置一个皇后,让你输出所有可能的答案. 经典的回溯 ...

  4. [2019HDU多校第二场][HDU 6591][A. Another Chess Problem]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6591 题目大意:二维坐标系上,所有满足\(5|2x+y\)的点都被设为障碍物,无法通过.现给出一对点, ...

  5. The 2016 ACMICPC Asia Beijing Regional Contest

    A. Harmonic Matrix Counter (3/19) B. Binary Tree (1/14) C. Asa's Chess Problem (21/65) [ Problem ] 给 ...

  6. HDU 5742 Chess SG函数博弈

    Chess Problem Description   Alice and Bob are playing a special chess game on an n × 20 chessboard. ...

  7. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  8. dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess

    Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...

  9. HDU 4405:Aeroplane chess(概率DP入门)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description   Hzz loves ...

随机推荐

  1. Java集合总结(三):堆与优先级队列

    堆 满二叉树:满二叉树是指,除了最后一层外,每个节点都有两个孩子,而最后一层都是叶子节点,都没有孩子. 完全二叉树:完全二叉树不要求最后一层是满的,但如果不满,则要求所有节点必须集中在最左边,从左到右 ...

  2. 把字符串当做js代码执行的方法

    字符串还能当做javascript代码来执行?你能想到哪些方法? 1.setInterval("要执行的字符串",500);window对象的方法既可以传字符串,也可以传函数.该函 ...

  3. maven+SSM+junit+jetty+log4j2环境配置的最佳实践

    思路大致是 jetty插件 -> junit -> SpringMVC -> Spring -> log4j2 -> Mybatis整合 pom中的依赖跟着思路一批一批的 ...

  4. MySQL数据分析(16)— 数据操作之增删改查

    前面我们说学习MySQL要从三个层面,四大逻辑来学,三个层面就是库层面,表层面和数据层面对吧,数据库里放数据表,表里放数据是吧,大家可以回忆PPT中jacky的这图,我们已经学完了库层面和表层面,从本 ...

  5. c#简单的SQLHelp

    public abstract class SQLHelper { //只读的静态数据库连接字符串 //需添加引用System.Configuration; public static readonl ...

  6. iTerm2 半透明颜色主题与字体配置

    下载iTerm2https://www.iterm2.com/ 安装. 下载这个主题https://raw.githubusercontent.com/mbadolato/iTerm2-Color-S ...

  7. elasticsearch _all

    在轻量搜索中,我们介绍了 _all 字段:一个把其它字段值 当作一个大字符串来索引的特殊字段. query_string 查询子句(搜索 ?q=john )在没有指定字段时默认使用 _all 字段._ ...

  8. asp.net core spa应用(angular) 部署同一网站下

    需求:现在一个应用是前后端开发分离,前端使用angular,后端使用 asp.net core 提供api ,开发完成后,现在需要把两个程序部署在同一个网站下,应该怎么处理? 首先可以参考微软的官方文 ...

  9. VMware下的CentOS7在桥接模式下,设置静态ip联网

    之前在虚拟机下联网都用的是NAT模式,但现在做一个项目需要实现在两台笔记本电脑中的VMware虚拟机中的CentOS联网, 从网上查资料显示比较常规的方式是桥接模式.第一次用桥接模式联网,在网上查了很 ...

  10. 牛顿法与拟牛顿法(四) BFGS 算法

    转自 https://blog.csdn.net/itplus/article/details/21897443