整数规划

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

  

HDU 6346 整数规划 (最佳完美匹配)的更多相关文章

  1. UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)

    UVA - 1349 Optimal Bus Route Design Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  2. UVa 11383 少林决胜(二分图最佳完美匹配)

    https://vjudge.net/problem/UVA-11383 题意: 给定一个N×N矩阵,每个格子里都有一个正整数W(i,j).你的任务是给每行确定一个整数row(i),每列也确定一个整数 ...

  3. UVaLive 4043 Ants (最佳完美匹配)

    题意:给定 n 个只蚂蚁和 n 棵树的坐标,问怎么匹配使得每个蚂蚁到树的连线不相交. 析:可以把蚂蚁和树分别看成是两类,那么就是一个完全匹配就好,但是要他们的连线不相交,那么就得考虑,最佳完美匹配是可 ...

  4. 【LA4043 训练指南】蚂蚁 【二分图最佳完美匹配,费用流】

    题意 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接一条线段. 分析 结点分黑白,很容易想到二分图.其中每个白点对应一个X结 ...

  5. Ants(二分图最佳完美匹配)

    Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6904   Accepted: 2164   Special Ju ...

  6. UVA - 1045 The Great Wall Game(二分图最佳完美匹配)

    题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...

  7. Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)

    题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...

  8. UVALive 4043 转化最佳完美匹配

    首先黑点和白点是组成一个二分图这毫无疑问 关键是题目中要求的所有黑白配的线不能交叉...一开始我也没想到这个怎么转化为二分图里面的算法. 后来看书才知道,如果两两交叉,则可以把两根线当四边形的对角线, ...

  9. ZOJ-3933 Team Formation (二分图最佳完美匹配)

    题目大意:n个人,分为两个阵营.现在要组成由若干支队伍,每支队伍由两个人组成并且这两个人必须来自不同的阵营.同时,每个人都有m个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ...

随机推荐

  1. 解题(LeatestCarFee -计算最少过路费)

    NowCoder今年买了一辆新车,他决定自己开车回家过年.回家过程中要经过ň个大小收费站,每个收费站的费用不同,你能帮他计算一下最少需要给多少过路费吗? 输入描述: 输入包含多组数据,每组数据第一行包 ...

  2. 【练习】Python第一,二次

    练习一 1,执行Python脚本的两种方式 a,Python解释器 b,Python  1.py 2,简述位和字节的关系 一个字节等于8位 3,简述ascii,unicode,utf-8,gbk的关系 ...

  3. PhoenixFD插件流体模拟——UI布局【Gird】详解

    流体网格 本文主要讲解Grid折叠栏中的内容 主要内容 Overview 综述 Parameters 参数 General 普通参数 Example: Scene Scale Example: Gri ...

  4. Java第二周作业

    Java第二周作业 本周作业: 参考http://www.cnblogs.com/rocedu/p/7911138.html 学习第二三章视频 参考http://www.cnblogs.com/roc ...

  5. Opencv-Python学习笔记(一)

    学习和研究计算机视觉,必然绕不开OpenCV. 于是我下载了它的C++源码,用cmake编译遇到一些错误. 然后结合网上一些帖子看源码看了好几天,发现有点不知从何处入手. 于是准备从其python版本 ...

  6. 51-python3 pandas读写excel

    转载自:https://blog.csdn.net/brink_compiling/article/details/76890198?locationNum=7&fps=1 0. 前言Pyth ...

  7. Day2数据结构和算法

    2019-02-28,10:23:52 算法效率的度量方法 事后统计方法:为每一个程序编制测试程序 ,比较时间.(很麻烦,没有普遍适用性) 事前分析估算方法:在计算机程序编写前,依据统计方法对算法进行 ...

  8. Spring Scheduled定时任务报错 java.lang.IllegalStateException: Encountered invalid @Scheduled method 'xxx': For input string: "2S"

    报错信息如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ding ...

  9. stm32定时器时钟以及中间对齐模式

    在永磁同步电机的控制中,需要对电机的三相定子施加一定的电压,才能控制电机转动.现在用的较多的是SVPWM(SVPWM的具体原理会在后面另写一篇博客说明),要想产生SVPWM波形,需要控制的三相电压呈如 ...

  10. Python MD5算法使用

    ## md5算法简介 1.  **简介**   MD5消息摘要算法(MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值 ...