Live2D

Solution -「CF 802C」Heidi and Library (hard)

Descriptoin

  Link.

  你有一个容量为 k 的空书架,现在共有 n 个请求,每个请求给定一本书 ai。如果你的书架里没有这本书,你就必须以 cai 的价格购买这本书放入书架。当然,你可以在任何时候丢掉书架里的某本书。请求出完成这 n 个请求所需要的最少价钱。

  n,k80

Solution

  网络瘤嘛……

  费用流,考虑先全部买入,再抵消花费。具体地,假设 i<j,第 i 天的书和第 j 天的书相同,就可以一直保留第 i 天的书到第 j 天,减少一次花费。脑洞一番之后,建图如下:

  • Svi (i=1,2,n) 连边,容量为 1,费用为 cai,表示买入。
  • vivi+1 (i=1,2,,n1) 连边,容量为 k1,费用为 0,表示保留至多 k1 本,剩下一本给 ai+1 留位置。
  • vivi (i=1,2,,n) 连边,容量为 1,费用为 0,表示将这本书出手(丢掉或卖掉)。
  • vi1 向上一次 ai 出现的位置 j 所对应的 vj 连边,容量为 1,费用为 cai,表示上次的“出手”是卖掉,以抵消 本次 ai 的花费。
  • viT 连边,容量为 1,费用为 0

  费用流求出的最小费用就是答案。

Code

#include <queue>
#include <cstdio>

typedef std::pair<int, int> pii;

const int MAXN = 80, MAXND = MAXN * 2 + 2, MAXED = 5 * MAXN, INF = 0x3f3f3f3f;
int n, K, S, T, ecnt = 1, a[MAXN + 5], c[MAXN + 5], las[MAXN + 5];
int head[MAXND + 5], curh[MAXND + 5], d[MAXND + 5];
bool vis[MAXND + 5];

struct Edge { int to, flow, cost, nxt; } graph[MAXED * 2 + 5];

inline void link ( const int s, const int t, const int f, const int c ) {
	graph[++ ecnt] = { t, f, c, head[s] };
	head[s] = ecnt;
}

inline void addEdge ( const int s, const int t, const int f, const int c ) {
	link ( s, t, f, c ), link ( t, s, 0, -c );
}

inline pii DFS ( const int u, int iflow ) {
	vis[u] = true;
	if ( u == T ) return { iflow, 0 };
	int oflow = 0, ocost = 0;
	for ( int& i = curh[u], v; i; i = graph[i].nxt ) {
		if ( ! vis[v = graph[i].to] && d[v] == d[u] + graph[i].cost && graph[i].flow ) {
			pii of ( DFS ( v, std::min ( iflow, graph[i].flow ) ) );
			oflow += of.first, ocost += of.first * graph[i].cost + of.second;
			graph[i].flow -= of.first, graph[i ^ 1].flow += of.first;
			if ( ! ( iflow -= of.first ) ) break;
		}
	}
	if ( ! oflow ) d[u] = INF;
	return { oflow, ocost };
}

inline bool SPFA () {
	static std::queue<int> que;
	static bool inq[MAXND + 5];
	for ( int i = 0; i <= T; ++ i ) d[i] = INF, inq[i] = false;
	que.push ( S ), d[S] = 0, inq[S] = true;
	for ( int u; ! que.empty (); ) {
		inq[u = que.front ()] = false, que.pop ();
		for ( int i = head[u], v; i; i = graph[i].nxt ) {
			if ( d[v = graph[i].to] > d[u] + graph[i].cost && graph[i].flow ) {
				d[v] = d[u] + graph[i].cost;
				if ( ! inq[v] ) que.push ( v ), inq[v] = true;
			}
		}
	}
	return d[T] ^ INF;
}

inline int Dinic () {
	int ret = 0;
	for ( ; SPFA (); ret += DFS ( S, INF ).second ) {
		for ( int i = 0; i <= T; ++ i ) {
			vis[i] = false;
			curh[i] = head[i];
		}
	}
	return ret;
}

int main () {
	scanf ( "%d %d", &n, &K );
	for ( int i = 1; i <= n; ++ i ) scanf ( "%d", &a[i] );
	for ( int i = 1; i <= n; ++ i ) scanf ( "%d", &c[i] );
	S = 0, T = n << 1 | 1;
	for ( int i = 1; i <= n; ++ i ) {
		addEdge ( S, i, 1, c[a[i]] ), addEdge ( i, i + n, 1, 0 );
		if ( las[a[i]] ) addEdge ( i - 1, las[a[i]] + n, 1, -c[a[i]] );
		if ( i < n ) addEdge ( i, i + 1, K - 1, 0 );
		addEdge ( i + n, T, 1, 0 ), las[a[i]] = i;
	}
	printf ( "%d\n", Dinic () );
	return 0;
}
posted @   Rainybunny  阅读(64)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示