2020杭电多校第二场 New Equipments(费用流)

2024-04-16 00:38

本文主要是介绍2020杭电多校第二场 New Equipments(费用流),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem Description
Little Q’s factory recently purchased m pieces of new equipment, labeled by 1,2,…,m.

There are n workers in the factory, labeled by 1,2,…,n. Each worker can be assigned to no more than one piece of equipment, and no piece of equipment can be assigned to multiple workers. If Little Q assigns the i-th worker to the j-th piece of equipment, he will need to pay ai×j2+bi×j+ci dollars.

Now please for every k (1≤k≤n) find k pairs of workers and pieces of equipment, then assign workers to these pieces of equipment, such that the total cost for these k workers is minimized.

Input
The first line of the input contains a single integer T (1≤T≤10), the number of test cases.

For each case, the first line of the input contains two integers n and m (1≤n≤50, n≤m≤108), denoting the number of workers and the number of pieces of equipment.

Each of the following n lines contains three integers ai,bi and ci (1≤ai≤10, −108≤bi≤108, 0≤ci≤1016, b2i≤4aici), denoting a worker.

Output
For each test case, output a single line containing n integers, the k-th (1≤k≤n) of which denoting the minimum possible total cost for k pairs of workers and pieces of equipment.

Sample Input
1
3 5
2 3 10
2 -3 10
1 -1 4

Sample Output
4 15 37

Source
2020 Multi-University Training Contest 2

仍然是个网络流小白,嘤嘤嘤。

题意:
n个人,m个机器。每个人有三个属性a,b,c。每个人要找一个机器,每个机器至多分配给一个人。第 i i i个人选了第 j j j个机器,则花费为 a [ i ] ∗ x 2 + b [ i ] ∗ x + c [ i ] a[i]*x^2+b[i]*x+c[i] a[i]x2+b[i]x+c[i]
求最小花费。

思路:
参考了hl大佬的博客:https://www.cnblogs.com/lonely-wind-/p/13368156.html

这是一个二次函数,而且是个凹函数(a>0)。

那么我们对每个人,直接三分找对应机器的最小值,然后从这个最小值点,往左右延伸 n − 1 n-1 n1个点,得到 n n n个预选点,然后向这个 n n n个点建一条流量为1,费用为对应花费的边。也就是人向机器连边。

最后确定一个源点,向所有人连边,确定一个汇点,将所有机器向汇点连边。

跑一个最小费用最大流即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<unordered_map>using namespace std;
typedef long long ll;
const int N = 5005, M = 200010;
ll ver[M], edge[M], cost[M], Next[M], head[N];
ll d[N], incf[N], pre[N], v[N];
ll n, k, tot, s, t, maxflow, ans;void add(int x, int y, int z, ll c) {// 正向边,初始容量z,单位费用cver[++tot] = y, edge[tot] = z, cost[tot] = c;Next[tot] = head[x], head[x] = tot;// 反向边,初始容量0,单位费用-c,与正向边“成对存储”ver[++tot] = x, edge[tot] = 0, cost[tot] = -c;Next[tot] = head[y], head[y] = tot;
}bool spfa() {queue<int> q;memset(d, 0x3f, sizeof(d)); // -INFmemset(v, 0, sizeof(v));q.push(s); d[s] = 0; v[s] = 1; // SPFA 求最长路incf[s] = 1ll << 62; // 增广路上各边的最小剩余容量while (q.size()) {int x = q.front(); v[x] = 0; q.pop();for (int i = head[x]; i; i = Next[i]) {if (!edge[i]) continue; // 剩余容量为0,不在残量网络中,不遍历int y = ver[i];if (d[y]>d[x] + cost[i]) {d[y] = d[x] + cost[i];incf[y] = min(incf[x], edge[i]);pre[y] = i; // 记录前驱,便于找到最长路的实际方案if (!v[y]) v[y] = 1, q.push(y);}}}if (d[t] == 0x3f3f3f3f3f3f3f3f) return false; // 汇点不可达,已求出最大流return true;
}// 更新最长增广路及其反向边的剩余容量
void update() {int x = t;while (x != s) {int i = pre[x];edge[i] -= incf[t];edge[i ^ 1] += incf[t]; // 利用“成对存储”的xor 1技巧x = ver[i ^ 1];}maxflow += incf[t];ans += d[t] * incf[t];
}int match[55][55];
ll a[55],b[55],c[55];void init() {memset(head,0,sizeof(head));memset(pre,0,sizeof(pre));memset(incf,0,sizeof(incf));tot = 1;ans = 0;
}ll f(int i,ll x) {return a[i] * x * x + b[i] * x + c[i];
}int main() {int T;scanf("%d",&T);while(T--) {scanf("%d%d",&n,&k);init();for(int i = 1;i <= n;i++) {scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);int l = 1,r = k;int pos = 1;while(l < r) {int m1 = (2 * l + r) / 3;int m2 = (l + 2 * r + 2) / 3;if(f(i,m1) > f(i,m2)) {l = m1 + 1;} else {r = m2 - 1;}}if(f(i,l) < f(i,r)) {pos = l;} else {pos = r;}l = pos - 1,r = pos + 1;match[i][1] = pos;for(int j = 2;j <= n;j++) {if(f(i,l) < f(i,r)) {if(l >= 1 && l <= k) {match[i][j] =l;l--;} else {match[i][j] = r;r++;}} else {if(r >= 1 && r <= k) {match[i][j] = r;r++;} else {match[i][j] = l;l--;}}}}unordered_map<int,int>mp;int cnt = 0;for(int i = 1;i <= n;i++) {for(int j = 1;j <= n;j++) {if(!mp[match[i][j]]) {mp[match[i][j]] = ++cnt;}}}s = 0,t = n + cnt + 1;for(int i = 1;i <= n;i++) {add(s,i + cnt,1,0);for(int j = 1;j <= n;j++) {add(i + cnt,mp[match[i][j]],1,f(i,match[i][j]));}}for(int i = 1;i <= cnt;i++) {add(i,t,1,0);}for(int i = 1;i <= n;i++) {spfa();update();printf("%lld",ans);printf("%c",i == n ? '\n' : ' ');}}return 0;
}

这篇关于2020杭电多校第二场 New Equipments(费用流)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907361

相关文章

Golan中 new() 、 make() 和简短声明符的区别和使用

《Golan中new()、make()和简短声明符的区别和使用》Go语言中的new()、make()和简短声明符的区别和使用,new()用于分配内存并返回指针,make()用于初始化切片、映射... 详细介绍golang的new() 、 make() 和简短声明符的区别和使用。文章目录 `new()`

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 2175 最小费用最大流TLE

题意: 一条街上有n个大楼,坐标为xi,yi,bi个人在里面工作。 然后防空洞的坐标为pj,qj,可以容纳cj个人。 从大楼i中的人到防空洞j去避难所需的时间为 abs(xi - pi) + (yi - qi) + 1。 现在设计了一个避难计划,指定从大楼i到防空洞j避难的人数 eij。 判断如果按照原计划进行,所有人避难所用的时间总和是不是最小的。 若是,输出“OPETIMAL",若

poj 2135 有流量限制的最小费用最大流

题意: 农场里有n块地,其中约翰的家在1号地,二n号地有个很大的仓库。 农场有M条道路(双向),道路i连接着ai号地和bi号地,长度为ci。 约翰希望按照从家里出发,经过若干块地后到达仓库,然后再返回家中的顺序带朋友参观。 如果要求往返不能经过同一条路两次,求参观路线总长度的最小值。 解析: 如果只考虑去或者回的情况,问题只不过是无向图中两点之间的最短路问题。 但是现在要去要回

poj 3422 有流量限制的最小费用流 反用求最大 + 拆点

题意: 给一个n*n(50 * 50) 的数字迷宫,从左上点开始走,走到右下点。 每次只能往右移一格,或者往下移一格。 每个格子,第一次到达时可以获得格子对应的数字作为奖励,再次到达则没有奖励。 问走k次这个迷宫,最大能获得多少奖励。 解析: 拆点,拿样例来说明: 3 2 1 2 3 0 2 1 1 4 2 3*3的数字迷宫,走两次最大能获得多少奖励。 将每个点拆成两个

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为

poj 3068 有流量限制的最小费用网络流

题意: m条有向边连接了n个仓库,每条边都有一定费用。 将两种危险品从0运到n-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。 求最小的费用是多少。 解析: 抽象出一个源点s一个汇点t,源点与0相连,费用为0,容量为2。 汇点与n - 1相连,费用为0,容量为2。 每条边之间也相连,费用为每条边的费用,容量为1。 建图完毕之后,求一条流量为2的最小费用流就行了

最大流、 最小费用最大流终极版模板

最大流  const int inf = 1000000000 ;const int maxn = 20000 , maxm = 500000 ;struct Edge{int v , f ,next ;Edge(){}Edge(int _v , int _f , int _next):v(_v) ,f(_f),next(_next){}};int sourse , mee

cf 164 C 费用流

给你n个任务,k个机器,n个任务的起始时间,持续时间,完成任务的获利 每个机器可以完成任何一项任务,但是同一时刻只能完成一项任务,一旦某台机器在完成某项任务时,直到任务结束,这台机器都不能去做其他任务 最后问你当获利最大时,应该安排那些机器工作,即输出方案 具体建图方法: 新建源汇S T‘ 对任务按照起始时间s按升序排序 拆点: u 向 u'连一条边 容量为 1 费用为 -c,

java线程深度解析(一)——java new 接口?匿名内部类给你答案

http://blog.csdn.net/daybreak1209/article/details/51305477 一、内部类 1、内部类初识 一般,一个类里主要包含类的方法和属性,但在Java中还提出在类中继续定义类(内部类)的概念。 内部类的定义:类的内部定义类 先来看一个实例 [html]  view plain copy pu