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个厌恶的对象,并且厌恶是相互的.相互厌恶的人不能组成一支队伍.问最多能组成 ...
随机推荐
- yum管理工具
yum:也是一个rpm包的管理工具,可以实现自动解决rpm包的依赖关系(自动安装依赖顺序进行rpm包的安装) 为何rpm包会有依赖关系? 制作rpm的人,在制作rpm包的时候,就将这个rpm的所依赖的 ...
- Mongodb数据库学习
数据库 MongoDB (芒果数据库) 数据存储阶段 文件管理阶段 (.txt .doc .xls)优点 : 数据可以长期保存 可以存储大量的数据 使用简单 缺点 : 数据一致性差 数据查找修改不方便 ...
- OpenStack 安装:neutron服务
在上一篇中介绍了Nova的安装配置,这一篇介绍neutron 首先,创建neutron用户并设置密码为neutron [root@linux-node1 ~]# openstack user crea ...
- coderwarrior 查看程序大小 Code Size
https://mcuoneclipse.com/2012/09/24/code-size-information-with-gcc-for-armkinetis/
- Linux下Samba的安装和使用
一. samba的安装: sudo apt-get insall samba sudo apt-get install smbfs 二. 创建共享目录: mkdir /home/phinecos/sh ...
- 你使用的ie版本过低请。。。
放到body里面 <body> <!--[if lte IE 8]> <p class="chromeframe">您使用的IE浏览器版本过低. ...
- poj3250(单调栈模板题)
题目链接:https://vjudge.net/problem/POJ-3250 题意:求序列中每个点右边第一个>=自身的点的下标. 思路:简单介绍单调栈,主要用来求向左/右第一个小于/大于自身 ...
- Harry Potter and J.K.Rowling(半平面交+圆和矩形交)
Harry Potter and J.K.Rowling http://acm.hdu.edu.cn/showproblem.php?pid=3982 Time Limit: 2000/1000 MS ...
- Java中代理
普通代理(最简单的代理) 需要有两个实现同一个接口的类,一个是被代理的类,一个是代理类 被代理类中我们按照自己想实现的功能重写接口中的方法 代理类中因为需要代理被代理类,所以在代理类中需要有个被代理类 ...
- Centos7关闭防火墙
CentOS 7.0默认使用的是firewall作为防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewal ...