【UVA11383】 Golden Tiger Claw 【二分图KM算法(板子)】
2024-09-04 07:32:36
题目
题目传送门:https://www.luogu.com.cn/problem/UVA11383
分析
最近刚刚学了二分图,然后来了一个这样的题,看完题意之后,稍微想一想就能想出来是一个二分图,然后就是裸的板子解决。没什么难的。
代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std;
const int maxn=1e3+;
const int Inf=0x3f3f3f3f;
ll n,mi;
ll x[maxn],y[maxn],w[maxn][maxn],mat[maxn];
bool S[maxn],T[maxn];
bool Match(ll u){
S[u]=true;
for(ll i=;i<=n;++i){
if(!T[i]){
ll tmp(x[u]+y[i]-w[u][i]);
if(!tmp){
T[i]=true;
if(!mat[i] || Match(mat[i])){
mat[i]=u;
return true;
}
}
else
mi=min(mi,tmp);
}
}return false;
}
void Update(){
for(ll i=;i<=n;++i)
if(S[i]) x[i]-=mi;
for(ll i=;i<=n;++i)
if(T[i]) y[i]+=mi;
}
void KM(){
for(ll i=;i<=n;++i){
mat[i]=x[i]=y[i]=;
for(ll j=;j<=n;++j)
x[i]=max(x[i],w[i][j]);
}
for(ll i=;i<=n;++i){
while(true){
for(ll i=;i<=n;++i)
S[i]=T[i]=false;
mi=Inf;
if(Match(i)) break;
else Update();
}
}
}
int main(){
while(scanf("%d",&n)!=EOF&&n){
for(ll i=;i<=n;++i)
for(ll j=;j<=n;++j)
scanf("%d",&w[i][j]);
KM();
ll ans=;
for(ll i=;i<=n;++i)
ans+=x[i];
for(ll i=;i<=n;++i)
ans+=y[i]; printf("%d",x[]);
for(ll i=;i<=n;++i)
printf(" %d",x[i]);printf("\n");
printf("%d",y[]);
for(ll i=;i<=n;++i)
printf(" %d ",y[i]);printf("\n"); printf("%d\n",ans);
}return ;
}
最新文章
- Windows Azure Storage (20) 使用Azure File实现共享文件夹
- T-SQL中的随机数
- Endless Sky源码学习笔记-3
- 技术英文单词贴--R
- echo和print语句
- css 权威指南笔记(四)选择器
- formidable上传图片
- android变化HOLO对话风格
- 面试题-Java Web-网络通信
- BestCoder Round #86 A B C
- HTML+JS+JQuery不可以使用status
- 获得最近一天的提交,并使用winscp上传到服务器
- tomcat配置问题:访问http://localhost:8080/ 遇到 Access Error: 404
- Spring MVC+Mybatis 多数据源配置
- 第10课 C++中的新成员
- PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
- <;s:action>;的一些用法
- nc命令的常用参数介绍
- LTE:EPC
- jquery colsest的用法
热门文章
- Java实现 LeetCode 732 我的日程安排表 III(暴力 || 二叉树)
- (Java实现) 有重复元素排列问题
- Java实现 LeetCode 449 序列化和反序列化二叉搜索树
- Android中WebView如何加载JavaScript脚本
- Java实现台阶问题
- Java实现完美洗牌算法
- http1.0 、http1.1和http2.0的区别
- 批量执行app自动化测试思路设计图
- 使用Volo.Abp.MailKit发送邮件
- 记一次@ResponseBody注解返回中文乱码的问题