HDU 6346 整数规划 (最佳完美匹配)
整数规划
Time Limit: 5500/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 435 Accepted Submission(s): 144
给定 n×n 个整数 ai,j(1≤i,j≤n),要找出 2n 个整数 x1,x2,…,xn,y1,y2,…,yn 在满足 xi+yj≤ai,j(1≤i,j≤n) 的约束下最大化目标函数 ∑ni=1xi+∑ni=1yi,
你需要帮他解决这个整数规划问题,并给出目标函数的最大值。
接下来依次描述 T 组测试数据。对于每组测试数据:
第一行包含一个整数 n,表示该整数规划问题的规模。
接下来 n 行,每行包含 n 个整数,其中第 i 行第 j 列的元素是 ai,j。
保证 1≤T≤20,1≤n≤200,−109≤ai,j≤109(1≤i,j≤n)。
1
0
2
1 2
3 4
Case #2: 5
Statistic | Submit | Discuss | Note
析:根据题目给定的条件 xi+yj≤ai,j(1≤i,j≤n),这就是求最佳完全匹配(最小权值)的可行顶标的定义,所以直接就是一个裸的 KM 算法,因为是最小权值,所以只要把权值取反即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 10;
const int maxm = 1e6 + 10;
const LL mod = 998244353LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; } LL w[maxn][maxn], x[maxn], y[maxn], slack[maxn];
int prev_x[maxn], prev_y[maxn], son_y[maxn], par[maxn];
int lx, ly; void adjust(int v){
son_y[v] = prev_y[v];
if(prev_x[son_y[v]] != -2) adjust(prev_x[son_y[v]]);
} bool find(int v){
for(int i = 0; i < n; ++i) if(prev_y[i] == -1){
if(slack[i] > x[v] + y[i] - w[v][i]){
slack[i] = x[v] + y[i] - w[v][i];
par[i] = v;
}
if(x[v] + y[i] == w[v][i]){
prev_y[i] = v;
if(son_y[i] == -1){
adjust(i); return true;
}
if(prev_x[son_y[i]] != -1) continue;
prev_x[son_y[i]] = i;
if(find(son_y[i])) return true;
}
}
return false;
} LL KM(){
ms(son_y, -1); ms(y, 0);
for(int i = 0; i < n; ++i){
x[i] = 0;
for(int j = 0; j < n; ++j)
x[i] = max(x[i], w[i][j]);
}
bool flag;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
prev_x[j] = prev_y[j] = -1;
slack[j] = LNF;
}
prev_x[i] = -2;
if(find(i)) continue;
flag = false;
while(!flag){
LL m = LNF;
for(int j = 0; j < n; ++j)
if(prev_y[j] == -1) m = min(m, slack[j]);
for(int j = 0; j < n; ++j){
if(prev_x[j] != -1) x[j] -= m;
if(prev_y[j] != -1) y[j] += m;
else slack[j] -= m;
}
for(int j = 0; j < n; ++j) if(prev_y[j] == -1 && !slack[j]){
prev_y[j] = par[j];
if(son_y[j] == -1){
adjust(j);
flag = true;
break;
}
prev_x[son_y[j]] = j;
if(find(son_y[j])){
flag = true; break;
}
}
}
}
LL ans = 0;
for(int i = 0; i < n; ++i) ans += w[son_y[i]][i];
return ans;
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; w[i][j] = -w[i][j], ++j)
scanf("%I64d", &w[i][j]);
printf("Case #%d: %I64d\n", kase, -KM());
}
return 0;
}
HDU 6346 整数规划 (最佳完美匹配)的更多相关文章
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- UVa 11383 少林决胜(二分图最佳完美匹配)
https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...
- UVaLive 4043 Ants (最佳完美匹配)
题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交. 析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可 ...
- 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】
题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...
- Ants(二分图最佳完美匹配)
Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6904 Accepted: 2164 Special Ju ...
- UVA - 1045 The Great Wall Game(二分图最佳完美匹配)
题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...
- Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)
题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...
- UVALive 4043 转化最佳完美匹配
首先黑点和白点是组成一个二分图这毫无疑问 关键是题目中要求的所有黑白配的线不能交叉...一开始我也没想到这个怎么转化为二分图里面的算法. 后来看书才知道,如果两两交叉,则可以把两根线当四边形的对角线, ...
- ZOJ-3933 Team Formation (二分图最佳完美匹配)
题目大意:n个人,分为两个阵营.现在要组成由若干支队伍,每支队伍由两个人组成并且这两个人必须来自不同的阵营.同时,每个人都有m个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ...
随机推荐
- IDEA中显示RunDashboard
在打开.idea/workspace.xml,找到RunDashboard的配置,添加红色字体内容 <component name="RunDashboard"> &l ...
- 判断序列B是否是序列A的连续子序列
算法思想:因为两个整数序列已存入两个链表中,操作从两个链表的第一个结点开始,若对应得数据相等,则后移指针,若对应的数据不等,则A列表从上次开始比较结点的后继开始,B链表仍从第一个结点开始,直到B链表到 ...
- docker搭建gitlab服务器(Centos7)
系统环境:CentOS Linux release 7.6.1810 (Core) git版本:gitlab/gitlab-ce 一.安装和启动docker 见HTTPRUNNERMANAGER安装部 ...
- Tomcat的三种安装方式:解压版、安装版、配置成Windows服务版
https://blog.csdn.net/Jessica_XLF/article/details/81711429
- windows下git 同步数据到github的常见问题
常用排错方法: 1,查看连接是否正常. 2,push数据有时会报错,这是由于远程repository和我本地的repository冲突造成. 解决方法: 1.使用强制push的方法: git push ...
- 探究编译后,try-with-resources括号中的object是否关闭,以及两种写法编译后的对比
源码(@TargetApi(Build.VERSION_CODES.KITKAT)) public List<T> test1() { String sql = "selxe x ...
- gitlab-ci.xml:script config should be a string or an array of strings
The following command in a job script: STATUS_ID=$(grep -Eo "Status Code [0-9]+: Done" som ...
- vue项目中使用less或者sass的方法
半年木有更新博客了... 前段时间一直在学习vue,开始记录一下遇到的问题吧 这篇文章主要是总结一下vue中使用less或者sass的方法,以less为例(style.less) 主要是两种 1.对于 ...
- shell中脚本调试----学习
1.使用dos2unix命令处理在windows下开发的脚本 将windows下编辑的脚本放置到linux下执行的情况如下: [root@ks ~]# cat -v nginx.sh #!/bin/b ...
- ios 11 SDK 新特性 使用
Xcode 9虽然已经出了一段时间,但考虑到一些第三方库的适配,就没有升级.现在有时间了就升级到 Xcode 9,随便学习一下新的小技巧.感觉很好用哦~ 一.Named Color 关于更换主题的一个 ...