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 ...
随机推荐
- 全局路径规划算法Dijkstra(迪杰斯特拉算法)- matlab
参考博客链接:https://www.cnblogs.com/kex1n/p/4178782.html Dijkstra是常用的全局路径规划算法,其本质上是一个最短路径寻优算法.算法的详细介绍参考上述 ...
- Log4j 2使用教程二 【详解】
配置 Log4j 2的配置可以通过4种方式中的1种完成: 1.通过使用XML,JSON,YAML或属性格式编写的配置文件. 2.以编程方式,通过创建一个ConfigurationFactory和配置实 ...
- JS根据屏幕分辨率改变背景宽高
//控制浏览器显示的高宽 function document_loaded() { GotoMainStep(); /; document.getElementById("main1&quo ...
- Oarcle之group by关键字与having关键字
group by关键字 *group by :分组由 作用: 用于对于查询的数据进行分组并进行处理 例如:select deptno ,job from emp group by deptno, ...
- java static关键字和代码块
static关键字 代码块 方法重写 1. 方法重写的特点: 2. 注意事项: static关键字 为什么需要学习static关键字? 针对某一个变量属于类而不属于某一个具体的对象的时候,我们可以考虑 ...
- kafka学习-坑篇
安装(滤过) 启动(滤过) 坑(开始)--- topic creat完成后准备使用console-produce发布一个topic,错误如下: [-- ::,] WARN [Producer clie ...
- List、Set、Map的区别
(图一) 1.面试题:你说说collection里面有什么子类. (其实面试的时候听到这个问题的时候,你要知道,面试官是想考察List,Set) 正如图一,list和set是实现了collection ...
- UML类图新手入门级介绍(转)
首先,看动物矩形框,它代表一个类(Class).类图分三层,第一层显示类的名称,如果是抽象类,则就用斜体显示.第二层是类的特性,通常就是字段和属性.第三层是类的操作,通常是方法或行为.前面的符号,+ ...
- Bugku-CTF之过狗一句话(送给大家一个过狗一句话)
Day25 过狗一句话 http://123.206.87.240:8010/ 送给大家一个过狗一句话<?php $poc="a#s#s#e#r#t"; $poc_1=e ...
- [darknet]查看错误结果 sight of wrong
import os import numpy import cv2 bad_label_file = open("bad_valid.list",'r') names = [] f ...