XVI Open Cup named after E.V. Pankratiev. GP of Ekaterinburg--I.Iron man
n个服务器,k类任务,每个服务器完成一个任务都有特定的花费$cost_{i,j}$,但是你设置好某台机器去完成某项任务时他只能去完成这类任务,除非你可以花费$C$去更改配置。第$i$天要求去完成$q_{i,j}$个j类任务,问如何让总代价最小
用费用流去优化dp
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
typedef long long LL;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<P> VII;
const int inf = 1e9;
const int N = ;
int cost[][], a[][], dp[];
struct Edge {
int from, to, cap, flow, cost;
};
struct MCMF {
int n;
vector<Edge> edges;
vector<int> g[N];
int inq[N], d[N], p[N], a[N];
void add(int u, int v, int cap, int cost) {
edges.push_back((Edge){u, v, cap, , cost});
edges.push_back((Edge){v, u, , , -cost});
int m = edges.size();
g[u].push_back(m - );
g[v].push_back(m - );
}
bool spfa(int s, int t, int &flow, int &cost){
rep(i, , n) d[i] = inf;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = inf;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = ;
for (int i = ; i < g[u].size(); ++ i) {
Edge &e = edges[g[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = g[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to]) {
Q.push(e.to);
inq[e.to] = ;
}
}
}
}
if (d[t] == inf) return ;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u] ^ ].flow -= a[t];
u = edges[p[u]].from;
}
return ;
}
int minCost(int s, int t) {
int flow = , cost = ;
while (spfa(s, t, flow, cost)) continue; return cost;
}
}solver; int main() {
int n, k, c, m;
scanf("%d%d%d", &n, &k, &c); // n个服务器,k类任务
scanf("%d", &m);
memset(cost, -, sizeof(cost));
rep(i, , m) {
int s, t, w;
scanf("%d%d%d", &s, &t, &w);
cost[s][t] = w; // s 完成第t个任务的代价为w
}
int q;
scanf("%d", &q);
rep(i, , q) {
rep(j, , k) scanf("%d", &a[i][j]); // 第i天需要j服务器的数量
rep(j, , k) a[i][j] = a[i - ][j] + a[i][j];
}
auto calcCost = [&](int l, int r) -> int{
static int sum[];
rep(i, , k) sum[i] = a[r][i] - a[l - ][i];
// rep(i, 1, k) cout << sum[i] << ' '; cout << '\n';
rep(i, , solver.n) solver.g[i].clear();
solver.edges.clear();
// n 个服务器, k 类任务
int src = n + k + , dest = n + k + ;
solver.n = dest;
rep(i, , n) solver.add(src, i, , );
rep(i, , n) rep(j, , k)
if (cost[i][j] >= && sum[j]) solver.add(i, j + n, , cost[i][j] * sum[j]);
// 必须要第i个服务器可以完成第j类任务
rep(i, , k) solver.add(i + n, dest, sum[i] > , );
return solver.minCost(src, dest);
};
dp[] = ;
rep(i, , q) {
dp[i] = inf;
rep(j, , i - ) dp[i] = min(dp[i], dp[j] + calcCost(j + , i) + c);
}
printf("%d\n", dp[q]);
}
/*
5 5 10
12
1 1 63
2 1 37
2 2 12
3 2 98
5 2 57
2 3 74
3 3 55
4 3 32
1 4 62
3 4 18
2 5 21
4 5 42
10
97 20 29 95 46
51 32 96 53 37
60 85 50 23 94
92 11 53 26 46
66 64 18 0 58
18 2 3 97 37
17 11 73 7 93
36 30 31 27 51
22 35 31 77 1
83 68 66 64 64
*/
XVI Open Cup named after E.V. Pankratiev. GP of Ekaterinburg--I.Iron man的更多相关文章
- XVI Open Cup named after E.V. Pankratiev. GP of Ekaterinburg
A. Avengers, The 留坑. B. Black Widow 将所有数的所有约数插入set,然后求mex. #include<bits/stdc++.h> using names ...
- XVI Open Cup named after E.V. Pankratiev. GP of Ukraine
A. Associated Vertices 首先求出SCC然后缩点,第一次求出每个点能到的点集,第二次收集这些点集即可,用bitset加速,时间复杂度$O(\frac{nm}{64})$. #inc ...
- XVI Open Cup named after E.V. Pankratiev. GP of Peterhof
A. (a, b)-Tower 当指数大于模数的时候用欧拉定理递归计算,否则直接暴力计算. #include<cstdio> #include<algorithm> #incl ...
- XVI Open Cup named after E.V. Pankratiev. GP of Siberia
A. Passage 枚举两个点,看看删掉之后剩下的图是否是二分图. #include <bits/stdc++.h> using namespace std ; const int MA ...
- XVI Open Cup named after E.V. Pankratiev. GP of Eurasia
A. Nanoassembly 首先用叉积判断是否在指定向量右侧,然后解出法线与给定直线的交点,再关于交点对称即可. #include<bits/stdc++.h> using names ...
- XVI Open Cup named after E.V. Pankratiev. GP of SPB
A. Bubbles 枚举两个点,求出垂直平分线与$x$轴的交点,答案=交点数+1. 时间复杂度$O(n^2\log n)$. #include<cstdio> #include<a ...
- XV Open Cup named after E.V. Pankratiev. GP of Tatarstan
A. Survival Route 留坑. B. Dispersed parentheses $f[i][j][k]$表示长度为$i$,未匹配的左括号数为$j$,最多的未匹配左括号数为$k$的方案数. ...
- XVII Open Cup named after E.V. Pankratiev. GP of SPb
A. Array Factory 将下标按前缀和排序,然后双指针,维护最大的右边界即可. #include<cstdio> #include<algorithm> using ...
- XIV Open Cup named after E.V. Pankratiev. GP of SPb
A. Bracket Expression 直接按题意模拟即可. 时间复杂度$O(n)$. #include<stdio.h> #include<algorithm> #inc ...
随机推荐
- 简单的Json数据
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- Linux内核开发进阶书籍推荐(不适合初学者)
Linux内核开发进阶书籍推荐(不适合初学者) 很早之前就想写一篇文章总结一下Linux Kernel开发的相关资料,项目的原因,再加上家里的一些事情,一直没能找到闲暇,今天终于有些时间,希望可以完成 ...
- Django的admin管理系统写入中文出错的解决方法/1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation ‘locate’
Django的admin管理系统写入中文出错的解决方法 解决错误: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and ( ...
- CentOS7运行报错kernel:NMI watchdog: BUG: soft lockup - CPU#0 stuck for 26s
CentOS内核,对应的文件是/proc/sys/kernel/watchdog_thresh.CentOS内核和标准内核还有一个地方不一样,就是处理CPU占用时间过长的函数,CentOS下是watc ...
- laravel5.7 前后端分离开发 实现基于API请求的token认证
最近在学习前后端分离开发,发现 在laravel中实现前后台分离是无法无法使用 CSRF Token 认证的.因为 web 请求的用户认证是通过Session和客户端Cookie的实现的,而前后端分离 ...
- Qt框架及模块认识
小白自工作就接触Qt,一直都在使用Qt5.3.1版本,所以没有经历过大牛们把项目从Qt4程序到Qt5的烦恼,没准以后会碰到.对Qt所有的丰富的API表示惊叹,对于Qt的框架及模块认识也是极为模糊的,文 ...
- react 父子组件通信
import React from 'react'; import B from './B'; class A extends React.Component{ state = { msg:'我来自于 ...
- ACTIVEMQ 实例化到MSSQL
实例化文章很多,不重复,自行查询 直接上XML <!-- Licensed to the Apache Software Foundation (ASF) under one or more c ...
- Linux命令-cut篇
Cut 命令是常用的 Linux 命令,在这里总结一下平时常用的参数和用法,方便查证. 常用参数: -b:以字节为单位进行分割: -c:以字符为单位进行分割: -d:自定义分割符进行分割,默认为制表符 ...
- deno深入揭秘及未来展望
deno node.js之父Ryan Dahl在一个月前发起了名为deno的项目,项目的初衷是打造一个基于v8引擎的安全的TypeScript运行时,同时实现HTML5的基础API.所谓的安全运行时, ...