HDU 6346 整数规划 (最佳完美匹配)

时间:2023-03-08 16:07:56

整数规划

Time Limit: 5500/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 435    Accepted Submission(s): 144

Problem Description
度度熊有一个可能是整数规划的问题:

给定 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,

你需要帮他解决这个整数规划问题,并给出目标函数的最大值。

Input
第一行包含一个整数 T,表示有 T 组测试数据。

接下来依次描述 T 组测试数据。对于每组测试数据:

第一行包含一个整数 n,表示该整数规划问题的规模。

接下来 n 行,每行包含 n 个整数,其中第 i 行第 j 列的元素是 ai,j。

保证 1≤T≤20,1≤n≤200,−109≤ai,j≤109(1≤i,j≤n)。

Output
对于每组测试数据,输出一行信息 "Case #x: y"(不含引号),其中 x 表示这是第 x 组测试数据,y 表示目标函数的最大值,行末不要有多余空格。
Sample Input
2
1
0
2
1 2
3 4
Sample Output
Case #1: 0
Case #2: 5
Source
Recommend
chendu

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;
}