首页 > 其他分享 >AtCoder Beginner Contest 224 H Security Camera 2

AtCoder Beginner Contest 224 H Security Camera 2

时间:2023-11-03 21:44:45浏览次数:43  
标签:AtCoder Beginner Contest ll flow edges vis cost len

洛谷传送门

AtCoder 传送门

直接糊一手线性规划对偶板板。

要求:

\[\min \sum A_i l_i + \sum B_i r_i \]

\[\forall i, j, l_i + r_j \ge C_{i, j} \]

\[l_i, r_i \ge 0 \]

\[l_i, r_i \in \mathbb{Z} \]

可以证明 \(l_i, r_i\) 为整数的限制可以去掉,因为取到最优解时 \(l_i, r_i\) 一定顶着整数。

对偶相当于变量和限制互换,\(\min, \max\) 互换,不等式符号改变,变成:

\[\max \sum w_{i, j} C_{i, j} \]

\[\sum\limits_j w_{i, j} \le A_i \]

\[\sum\limits_i w_{i, j} \le B_j \]

我去这不是费用流板板吗。\(w_{i, j}\) 相当于 \(i \to j'\) 的流量,\(C_{i, j}\) 相当于这条边的费用,\(\le A_i\) 和 \(\le B_j\) 的限制相当于和源点与汇点的边的容量。那么:

  • 连边 \(S \to i\),容量为 \(A_i\),费用为 \(0\);
  • 连边 \(i \to j'\),容量为 \(+\infty\),费用为 \(C_{i, j}\);
  • 连边 \(j \to T\),容量为 \(B_i\),费用为 \(0\)。

跑一遍最大费用最大流即可。

最大费用最大流可以把费用全部取反跑最小费用最大流,最后把费用再取反。

code
// Problem: H - Security Camera 2
// Contest: AtCoder - AtCoder Beginner Contest 224
// URL: https://atcoder.jp/contests/abc224/tasks/abc224_h
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 110;
const int maxm = 1000100;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;

ll n, m, a[maxn], b[maxn], c[maxn][maxn];
ll head[maxm], len = 1, S, T, ntot, id[maxn][2];

struct edge {
	ll to, next, cap, flow, cost;
} edges[maxm];

inline void add_edge(ll u, ll v, ll c, ll f, ll co) {
	edges[++len].to = v;
	edges[len].next = head[u];
	edges[len].cap = c;
	edges[len].flow = f;
	edges[len].cost = co;
	head[u] = len;
}

struct MCMF {
	ll d[maxm], cur[maxm];
	bool vis[maxm];
	
	inline void add(ll u, ll v, ll c, ll co) {
		add_edge(u, v, c, 0, co);
		add_edge(v, u, 0, 0, -co);
	}
	
	inline bool spfa() {
		queue<int> q;
		for (int i = 1; i <= ntot; ++i) {
			d[i] = inf;
			vis[i] = 0;
		}
		q.push(S);
		vis[S] = 1;
		d[S] = 0;
		while (q.size()) {
			int u = q.front();
			q.pop();
			vis[u] = 0;
			for (int i = head[u]; i; i = edges[i].next) {
				edge &e = edges[i];
				if (d[e.to] > d[u] + e.cost && e.cap > e.flow) {
					d[e.to] = d[u] + e.cost;
					if (!vis[e.to]) {
						vis[e.to] = 1;
						q.push(e.to);
					}
				}
			}
		}
		return d[T] < inf;
	}
	
	ll dfs(ll u, ll a, ll &cost) {
		if (u == T || !a) {
			return a;
		}
		vis[u] = 1;
		ll flow = 0, f;
		for (ll &i = cur[u]; i; i = edges[i].next) {
			edge &e = edges[i];
			if (!vis[e.to] && d[e.to] == d[u] + e.cost && e.cap > e.flow) {
				if ((f = dfs(e.to, min(a, e.cap - e.flow), cost)) > 0) {
					cost += f * e.cost;
					e.flow += f;
					edges[i ^ 1].flow -= f;
					flow += f;
					a -= f;
					if (!a) {
						break;
					}
				}
			}
		}
		vis[u] = 0;
		return flow;
	}
	
	inline pii solve() {
		ll flow = 0, cost = 0;
		while (spfa()) {
			for (int i = 1; i <= ntot; ++i) {
				cur[i] = head[i];
			}
			flow += dfs(S, inf, cost);
		}
		return mkp(flow, cost);
	}
} solver;

void solve() {
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
		id[i][0] = ++ntot;
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%lld", &b[i]);
		id[i][1] = ++ntot;
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			scanf("%lld", &c[i][j]);
		}
	}
	S = ++ntot;
	T = ++ntot;
	for (int i = 1; i <= n; ++i) {
		solver.add(S, id[i][0], a[i], 0);
	}
	for (int i = 1; i <= m; ++i) {
		solver.add(id[i][1], T, b[i], 0);
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			solver.add(id[i][0], id[j][1], inf, -c[i][j]);
		}
	}
	pii ans = solver.solve();
	printf("%lld\n", -ans.scd);
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:AtCoder,Beginner,Contest,ll,flow,edges,vis,cost,len
From: https://www.cnblogs.com/zltzlt-blog/p/17808564.html

相关文章

  • AtCoder Beginner Contest(abc) 314
    B-Roulette难度:⭐题目大意有一个猜数字的游戏,有n个人参加,每人都猜了若干个数;最后给出答案数字;在所有猜中数字的人中输出猜数数量最少的人的编号;(可能不止一个);解题思路数据不大,暴力即可;神秘代码#include<bits/stdc++.h>#defineintlonglong#def......
  • AtCoder Beginner Contest 326 F
    F-RobotRotation一句话不开LL,见祖宗感谢大佬,和洛谷上的题解上面已经将的很清楚了,但是如果你跟我一样一开始看不懂他们的代码,那么这篇可能为你解惑点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineLLlonglong#defineintLL//LL!map<LL,LL>ma;......
  • AtCoder Beginner Contest(abc) 312
    B-TaKCode难度:⭐题目大意题目定义一种矩阵X:该矩阵的是一个长度为9的由黑白色块组成正方形矩阵;该矩阵的左上角和右下角都是一个3*3的黑色矩阵(总共18个),这两个黑色矩阵外面(边缘不算)包围一圈白色色块(总共14个);现在一个n*m的黑白矩阵,问这个大矩阵中有多少......
  • AT_abc326_e Revenge of "The Salary of AtCoder Inc." 题解
    AT_abc326_eRevengeof"TheSalaryofAtCoderInc."题解一道简单的概率论+动态规划题目(然而我赛时没看这道题题意有一个长度为\(n\)的序列\(A\)、一个\(n\)面骰子,掷若干次骰子,如果这一次掷骰子的点数小于等于上一次的点数,则结束。定义这若干次掷骰子的总的结果为,每次......
  • AtCoder Beginner Contest(abc) 311
    B-VacationTogether难度:⭐题目大意给定n个人的工作计划,'o'表示这天休息,'x'表示工作;请找出一段最长的所有人都休息的连续休息的天数;解题思路数据不大,暴力即可;神秘代码#include<bits/stdc++.h>#defineintlonglong#defineIOSios::sync_with_stdio......
  • AtCoder Beginner Contest 321(ABC321)
    A.321-likeChecker直接模拟。CodeB.Cutoff直接暴力枚举\([0\sim100]\),每次把第\(n\)个数当作当前枚举的\(i\),然后看看条件是否满足。CodeC.321-likeSearcherDescription给你一个\(K\),求出\([1\simK]\)区间内有多少个321-likeNumber。321-likeNumber的......
  • Atcoder Beginner Contest 326 (ABC326)
    不知道为什么拖到现在,我是摆怪。A.2UP3DOWN模拟,略。B.326-likeNumbers模拟,略。C.Peak双指针板子。D.ABCPuzzle基础dfs。但是赛时不知道为什么觉得状态数不会很少,于是写了一个巨大复杂的状压。这里粗略算算有效状态数:仅考虑每行的限制,有\(\binom{3}{5}=10\)种选......
  • AtCoder Beginner Contest 326
    A-2UP3DOWN(abc326A)题目大意100楼层,一次可以上最多两层,或下最多三层。给定两楼层,问能否一次到达。解题思路比较大小,然后判断其差是是不是在\(2\)或\(3\)内即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;intmain(void){......
  • AtCoder Beginner Contest 326 题解
    首先,\(\text{HappyBirthdaytome!}\)A-2UP3DOWN常规ABCA...//If,oneday,Ifinallymanagetomakemydreamsareality...//Iwonder,willyoustillbetherebymyside?#include<bits/stdc++.h>#defineIOSios::sync_with_stdio(false)#defineTI......
  • 【杂题乱写】AtCoder-ARC114
    AtCoder-ARC114_ANotcoprime\(50\)内的质数只有\(15\)个,可能的答案也就只有\(2^{15}\)个,直接枚举。提交记录:Submission-AtCoderAtCoder-ARC114_BSpecialSubsets就是\(i\)与\(f_i\)连边,每个连通块都是基环树,一定能剥叶子变成环,所以答案就是连通块非空子集个数。......