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的更多相关文章

  1. 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 ...

  2. XVI Open Cup named after E.V. Pankratiev. GP of Ukraine

    A. Associated Vertices 首先求出SCC然后缩点,第一次求出每个点能到的点集,第二次收集这些点集即可,用bitset加速,时间复杂度$O(\frac{nm}{64})$. #inc ...

  3. XVI Open Cup named after E.V. Pankratiev. GP of Peterhof

    A. (a, b)-Tower 当指数大于模数的时候用欧拉定理递归计算,否则直接暴力计算. #include<cstdio> #include<algorithm> #incl ...

  4. XVI Open Cup named after E.V. Pankratiev. GP of Siberia

    A. Passage 枚举两个点,看看删掉之后剩下的图是否是二分图. #include <bits/stdc++.h> using namespace std ; const int MA ...

  5. XVI Open Cup named after E.V. Pankratiev. GP of Eurasia

    A. Nanoassembly 首先用叉积判断是否在指定向量右侧,然后解出法线与给定直线的交点,再关于交点对称即可. #include<bits/stdc++.h> using names ...

  6. XVI Open Cup named after E.V. Pankratiev. GP of SPB

    A. Bubbles 枚举两个点,求出垂直平分线与$x$轴的交点,答案=交点数+1. 时间复杂度$O(n^2\log n)$. #include<cstdio> #include<a ...

  7. XV Open Cup named after E.V. Pankratiev. GP of Tatarstan

    A. Survival Route 留坑. B. Dispersed parentheses $f[i][j][k]$表示长度为$i$,未匹配的左括号数为$j$,最多的未匹配左括号数为$k$的方案数. ...

  8. XVII Open Cup named after E.V. Pankratiev. GP of SPb

    A. Array Factory 将下标按前缀和排序,然后双指针,维护最大的右边界即可. #include<cstdio> #include<algorithm> using ...

  9. XIV Open Cup named after E.V. Pankratiev. GP of SPb

    A. Bracket Expression 直接按题意模拟即可. 时间复杂度$O(n)$. #include<stdio.h> #include<algorithm> #inc ...

随机推荐

  1. HTML表单简单练习

    代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  2. 新增职责 不能从IE进入的问题 此责任无可用函数 (转)

    此责任无可用函数(The Function Is Not Available Under The Responsibility) When attempting to navigate to a fu ...

  3. android 开发设计模式---Strategy模式

    假设我们要出去旅游,而去旅游出行的方式有很多,有步行,有坐火车,有坐飞机等等.而如果不使用任何模式,我们的代码可能就是这样子的. 12345678910111213141516171819202122 ...

  4. redis 哈希(hash)函数

    哈希(hash)函数 hSet 命令/方法/函数 Adds a value to the hash stored at key. If this value is already in the has ...

  5. intellij idea 官方帮助文档翻译(前言)

    对于java程序员来说,intellj idea这个编辑器,应该很多人都听说过,但用的人似乎并不是很多. 这也没办法,毕竟现在网上的教程,培训班授课,大多还是以eclipse为主. eclipse固然 ...

  6. redis的 rdb 和 aof 持久化的区别

    aof,rdb是两种 redis持久化的机制.用于crash后,redis的恢复. rdb的特性如下: Code: fork一个进程,遍历hash table,利用copy on write,把整个d ...

  7. spring mvc 配置之 context:annotation-config vs component-scan

    <context:annotation-config/> spring的注解@Resource.@Autowire, @Required and @Qualifier 等注解只是一个提示, ...

  8. JDBC中常用的接口

    JDBC常用的接口DriverManager 驱动管理器获得数据库链接 Connection 数据库链接接口 Statement 语句接口,用来静态操作SQL语句 PreparedStatement ...

  9. v-charts简介

    一, v-charts简介 在使用 echarts 生成图表时,经常需要做繁琐的数据类型转化.修改复杂的配置项,v-charts 的出现正是为了解决这个痛点.基于 Vue2.0 和 echarts 封 ...

  10. 编译原理---antlr实践+编译过程理解+课程理解知识点

    0.其他说明 0.0编译器分为前.中.后端,课上主要学的是前端.前端又分为词法分析(lexical analysis).语法分析(syntax analysis).语义分析(semantic anal ...