Hungary Algorithm国外板子
Codeforces 1107一题除了dp做法还有二分带权匹配O(n^3)做法,国外网友的板子时间非常优秀,但矩阵设定的事情并不是很懂……
//Codeforces 1107F
const int maxn = 505;
int n;
ll a, b, k, Matrix[maxn][maxn];
using T = ll;
T Hungary_Algorithm(int n, int m) {
vector<T> u(n + 1), v(m + 1);
vector<int> p(m + 1), way(m + 1);
for (int i = 1; i <= n; ++i) {
p[0] = i;
int j0 = 0;
vector<T> minv(m + 1, INF);
vector<char> used(m + 1, 0);
do {
used[j0] = 1;
int i0 = p[j0], j1 = 0;
T d = INF;
for (int j = 1; j <= m; ++j)
if (!used[j]) {
T cur = Matrix[i0][j] - u[i0] - v[j];
if (cur < minv[j])
minv[j] = cur, way[j] = j0;
if (minv[j] < d)
d = minv[j], j1 = j;
}
for (int j = 0; j <= m; ++j)
if (used[j])
u[p[j]] += d, v[j] -= d;
else
minv[j] -= d;
j0 = j1;
} while(p[j0] != 0);
do {
int j1 = way[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0);
}
vector<int> ans(n + 1);
for (int j = 1; j <= m; ++j)
ans[p[j]] = j;
T cost = -v[0];
return cost;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
rep(i, 1, n) {
cin >> a >> b >> k;
rep(j, 1, n) {
//Matrix表示第i种卡在距最后一天(j-1)天时借走能得到的钱数,本题需要最大费用故而取负
Matrix[i][j] = min(0LL, -(a - b * min((ll)j - 1, k)));
}
}
cout << -Hungary_Algorithm(n, n) << endl;
return 0;
}
Hungary Algorithm国外板子的更多相关文章
- Code[VS] 1022 覆盖 题解
Code[VS] 1022 覆盖 题解 Hungary Algorithm 题目传送门:Code[VS] 1022 题目描述 Description 有一个N×M的单位方格中,其中有些方格是水塘,其 ...
- poj3041
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12162 Accepted: 6620 Descri ...
- POJ 3041 Asteroids 二分图之最大匹配
题意:在一个网格中有若干个点,每一次可以清除一行或者一列,问最少几次可以将网格中的点全部清除. 思路:这个题是一个入门的最大匹配题(这个好像不是思路..).一般的方式就是将 行 看作集合A,列 看作集 ...
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- [板子]ISAP
ISAP求最大流,敲了一发板子,无压行,教程略去.转载请随意. #include <cstdio> #include <cstring> #include <algori ...
- [板子]倍增LCA
倍增LCA板子,没有压行,可读性应该还可以.转载请随意. #include <cstdio> #include <cstring> #include <algorithm ...
- OpenCASCADE Root-Finding Algorithm
OpenCASCADE Root-Finding Algorithm eryar@163.com Abstract. A root-finding algorithm is a numerical m ...
- codevs1022 覆盖[Hungary 二分图最大匹配]
codevs1022 覆盖 有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地.如果要用1×2的矩阵区覆盖(覆盖过程不容许有任何部分重叠)这个陆地,那么最多可以覆盖多少陆地面积. 输入描述 ...
- XidianOJ 1097 焊板子的xry111
题目描述 我们知道xry111不仅仅代码写的棒,硬件也不是一般人可以比拟的,比如他可以左手右手加上嘴里叼着一个烙铁一起工作,同时焊接三块板子!Orz,现在他的实验室知道他这么厉害了,准备让他焊接N块板 ...
随机推荐
- JavaScript学习第四天
1.自定义属性,使用好索引值 例子: 下面有一段js代码: <script> window.onload = function(){ var oBtn = document.getElem ...
- Gym - 100676F Palindrome —— 并查集
题目链接:https://vjudge.net/contest/155789#problem/E 题解: 由于是回文串,所以可以先将在对称位置的字符放在同一个集合(如果期间有两个非‘?’,且不相等,则 ...
- ansible-playbook 打通ssh无秘钥
建议参考: http://www.cnblogs.com/jackchen001/p/6514018.html 这个代码清晰,效果佳! 参考链接: http://www.cnblogs.com/cao ...
- Watir RAutomation VS AutoIt to deal with popup
Watir使用过程中,经常遇到的弹出框分为几类: 1. Alert 2. Confirm 3. Prompt 4. File select Watir 处理弹出框,有很多方法,可以应用 AutoIt来 ...
- Nagios监控Windows的网卡流量
Nagios监控Windows的网卡流量 使用/usr/local/nagios/libexec/中的check_traffic.sh,不但可以监控Linux的网卡流量,也可以监控Windows服务器 ...
- selenium_page_object
最简单的pageobject github地址:https://github.com/defnngj/selenium_page_objects
- 生产环境下JAVA进程高CPU占用故障排查---temp
问题描述:生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析:1,程序属于CPU密集型,和开发沟通过, ...
- 用于生成交易统计时间戳(常配合echarts走势图使用)
<?php //获取交易统计时间戳 时间段内每小时 public function getPayCountTimeHours($start_date,$end_date){ $data = ar ...
- C - Bear and Five Cards
Description A little bear Limak plays a game. He has five cards. There is one number written on each ...
- Flutter实战视频-移动电商-43.详细页_补充首页跳转到详细页
43.详细页_补充首页跳转到详细页 首页轮播点击到详细页 修改我们轮播这里的代码:SwiperDiy这个类这里的代码 return InkWell( onTap: (){ Application.ro ...