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 ...
随机推荐
- zabbix监控实战<3> 之自定义监控实例
第一章 自定义监控tcp状态 命令可以选择ss 或者 netstat ss打印基于socket的统计信息,实际运行下来,ss的速度要比netstat要快得多 1.1 tcp的十一种状态 ...
- 【转】Oracle EBS中查询Profile的各种SQL
参考 http://blog.csdn.net/pan_tian/article/details/7652968#t0 Using API FND_PROFILE.save to update pro ...
- 大兄dei,早点看清this吧
说道this,可以说是前端中很重要的问题之一了,也是面试或者笔试常考的问题.所以还是早点看清this吧,大兄dei. this是什么?为什么要存在? this关键字是js中最最复杂的机制之一.他被自动 ...
- 微信小程序中的rpx与移动设备物理像素
如下图: pt也称逻辑像素点,px物理像素点,1pt等于2px或者3px或更多; iphone6下面0.5pt=1px=1rpx; 使用rpx,小程序在不同设备分辨率下自行转换: PPI=物理像素开根 ...
- JavaFX-Application
JavaFX—Application 1.Application是JavaFX程序的入口,任何javafx应用程序程序都要继承该类并重写start()方法 public class TsetStage ...
- python,day3,函数基础-3
本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 1.函数基本语法及特性 函数是什么? 函数一词 ...
- IP通信基础学习第四周(上)
IP地址现在由因特网名字与号码指派公司ICANN进行分配,它是标志一个主机(或路由器)和一条链路的接口,其编址方法有:分类的IP地址.子网的划分.构成超网. 分类两级IP地址可以记为:IP::={&l ...
- ios开发蓝图
- ELK学习笔记之ELK搜集OpenStack节点日志
模板来自网络,模板请不要直接复制,先放到notepad++内调整好格式,注意缩进 部署架构 控制节点作为日志服务器,存储所有 OpenStack 及其相关日志.Logstash 部署于所有节点,收集本 ...
- 根据RadioButtonList动态显示隐藏Div
使用场景 今天在写项目的时候遇到一个需求,注册页面,用户先选择类型继而填表单,所以需要根据选择切换表单,使用的前端框架是MiniUI,但是在实现这个功能的时候mini.get()方法无法得到div元素 ...