【UVA 11383】 Golden Tiger Claw (KM算法副产物)
Omi, Raymondo, Clay and Kimiko are on new adventure- in search of new Shen Gong Wu. But Evil
Boy Genius Jack Spicer is also there. Omi and Jack found the Shen Gong Wu at the same time so they
rushed for it but alas they touched it at the same time. Then what? It is time for “Xiaolin Showdown”.
Jack challenged Omi to play a game. The game is simple! There will be an N ∗ N board where
each cell in the board contains some number. They have to assign numbers to each row and column
separately so that w(i, j) ≤ row(i) + col(j) where w(i, j) is the number assigned to the cell located
at i-th row and j-th column, row(i) is the number assigned to i-th row and col(j) is the number
∑
assigned to j-th column. That is simple isnt it? Well . . . the main part is that you have to minimize
1≤i≤n
(row(i) + col(j)).
Jack has taken his favorite “Monkey Stuff” and Omi has taken “Golden Tiger Claw”. With the help
of this “Golden Tiger Claw”, he can go anywhere in the world. He has come to you and seeking your
help. Jack is using his computer to solve this problem. So do it quick! Find the most optimal solution
for Omi so that you can also be part of history in saving the world from the darkness of evil.
Input
Input contains 15 test cases. Each case starts with N. Then there are N lines containing N numbers
each. All the numbers in input is positive integer within the limit 100 except N which can be at most
500.
Output
For each case in the first line there will be N numbers, the row assignments. In the next line there
will N column assignment. And at the last line the minimum sum should be given. If there are several
possible solutions give any.
Note: Be careful about the output format. You may get Wrong Answer if you don’t output properly.
Sample Input
2
1 1
1 1
Sample Output
1 1
0 0
2
【题意】
给出一个n*n的矩阵(n<=500)给每一行x[i],每一列标号y[i],使得对任意a[i][j],x[i]+y[j]>=a[i][j]求行标与列标和最小
【分析】
事实上和最佳匹配没什么关系,但是我们进行KM算法的时候,有w(i,j)<=row(i)+col(j),并且算出来的顶标之和是最小的,so。。。
代码如下:
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- #include<cmath>
- using namespace std;
- #define Maxn 510
- #define Maxm 250010
- #define INF 0xfffffff
- struct node
- {
- int x,y,c,next;
- }t[Maxm];int len;
- int first[Maxn];
- int mymin(int x,int y) {return x<y?x:y;}
- int mymax(int x,int y) {return x>y?x:y;}
- void ins(int x,int y,int c)
- {
- t[++len].x=x;t[len].y=y;t[len].c=c;
- t[len].next=first[x];first[x]=len;
- }
- int a[Maxn][Maxn];
- int n;
- int lx[Maxn],ly[Maxn],match[Maxn],slack[Maxn];
- bool visx[Maxn],visy[Maxn];
- bool ffind(int x)
- {
- visx[x]=;
- for(int i=first[x];i;i=t[i].next) if(!visy[t[i].y])
- {
- int y=t[i].y;
- if(t[i].c==lx[x]+ly[y])
- {
- visy[y]=;
- if(!match[y]||ffind(match[y]))
- {
- match[y]=x;
- return ;
- }
- }
- else slack[y]=mymin(slack[y],lx[x]+ly[y]-t[i].c);
- }
- return ;
- }
- void solve()
- {
- memset(match,,sizeof(match));
- memset(lx,,sizeof(lx));
- memset(ly,,sizeof(ly));
- for(int i=;i<=n;i++)
- for(int j=first[i];j;j=t[j].next) lx[i]=mymax(lx[i],t[j].c);
- for(int i=;i<=n;i++)
- {
- for(int j=;j<=n;j++)
- slack[j]=INF;
- while()
- {
- memset(visx,,sizeof(visx));
- memset(visy,,sizeof(visy));
- if(ffind(i)) break;
- int delta=INF;
- for(int j=;j<=n;j++)
- {
- if(!visy[j])
- {
- delta=mymin(delta,slack[j]);
- }
- }
- if(delta==INF) return;
- for(int j=;j<=n;j++)
- {
- if(visx[j]) lx[j]-=delta;
- if(visy[j]) ly[j]+=delta;
- else slack[j]-=delta;
- }
- }
- }
- }
- int main()
- {
- while(scanf("%d",&n)!=EOF)
- {
- len=;
- memset(first,,sizeof(first));
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++)
- {
- int x;
- scanf("%d",&x);
- ins(i,j,x);
- }
- solve();
- for(int i=;i<=n;i++) printf("%d ",lx[i]);printf("\n");
- for(int i=;i<=n;i++) printf("%d ",ly[i]);printf("\n");
- int ans=;
- for(int i=;i<=n;i++) ans+=lx[i]+ly[i];
- printf("%d\n",ans);
- }
- return ;
- }
[UVA 11383]
2016-10-27 15:13:52
【UVA 11383】 Golden Tiger Claw (KM算法副产物)的更多相关文章
- UVA 11383 - Golden Tiger Claw(二分图完美匹配扩展)
UVA 11383 - Golden Tiger Claw 题目链接 题意:给定每列和每行的和,给定一个矩阵,要求每一个格子(x, y)的值小于row(i) + col(j),求一种方案,而且全部行列 ...
- UVA 11383 Golden Tiger Claw 金虎爪(KM算法)
题意: 给一个n*n的矩阵,每个格子中有正整数w[i][j],试为每行和每列分别确定一个数字row[i]和col[i],使得任意格子w[i][j]<=row[i]+col[j]恒成立.先输row ...
- 【KM算法】UVA 11383 Golden Tiger Claw
题目大意 给你一个\(n×n\)的矩阵G,每个位置有一个权,求两个一维数组\(row\)和\(col\),使\(row[i] + col[j]\ge G[i][j]\),并且\(∑row+∑col\) ...
- UVA11383 Golden Tiger Claw —— KM算法
题目链接:https://vjudge.net/problem/UVA-11383 题解: 根据KM()算法,标杆满足:l(x) + l(y) >= w(x, y) . 当求完最大权匹配之后,所 ...
- UVA 11383 Golden Tiger Claw 题解
题目 --> 题解 其实就是一个KM的板子 KM算法在进行中, 需要满足两个点的顶标值之和大于等于两点之间的边权, 所以进行一次KM即可. KM之后, 顶标之和就是最小的.因为如果不是最小的,就 ...
- UVA11383 Golden Tiger Claw KM算法
题目链接:传送门 分析 这道题乍看上去没有思路,但是我们仔细一想就会发现这道题其实是一个二分图最大匹配的板子 我们可以把这道题想象成将男生和女生之间两两配对,使他们的好感度最大 我们把矩阵中的元素\( ...
- Uva - 11383 - Golden Tiger Claw
题意:一个N*N的矩阵,第i行第j列的元素大小为w[i][j],每行求一个数row[i],每列求一个数col[j],使得row[i] + col[j] >= w[i][j],且所有的row[]与 ...
- UVA 11383 Golden Tiger Claw(最佳二分图完美匹配)
题意:在一个N*N的方格中,各有一个整数w(i,j),现在要求给每行构造row(i),给每列构造col(j),使得任意w(i,j)<=row(i)+col(j),输出row(i)与col(j)之 ...
- uva11383 Golden Tiger Claw 深入理解km算法
/** 题目: uva11383 Golden Tiger Claw 深入理解km算法 链接:https://vjudge.net/problem/UVA-11383 题意:lv 思路:lrj训练指南 ...
随机推荐
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
今天把notice表中的content字段改为long字段后,含有该字段的使用CTE的查询语句报错了.提示一下错误 ### Cause: java.sql.SQLException: 流已被关闭 ; ...
- JavaScript入门(7)
一.什么是函数 函数:把完成特定功能的代码放到一个函数里,直接调用这个函数,就省去重复输入大量代码的麻烦 函数的作用:写一次代码,然后反复地重用这个代码 Eg: 求多组数的和,不使用函数 { var ...
- CentOS7下用jdk1.7编译hadoop-2.7.1全过程详解
说实话,本人编译hadoop的过程比较曲折,但收获也很多,下面系统介绍一下CentOS7下编译hadoop-2.7.1的全过程吧. 先说明,32位Linux操作系统可以直接下载编译好的hadoop使用 ...
- XML操作 之获取指定节点值
根据节点名称快速查找 指定节点值 using (TextReader stringReader = new StringReader(clearPassResponse)) { XmlReaderSe ...
- Google Developers中国网站发布!(转)
Google Developers 中国网站是特别为中国开发者而建立的,它汇集了 Google 为全球开发者所提供的开发技术资源,包括 API 文档.开发案例.技术培训的视频.并涵盖了以下关键开发技术 ...
- JQuery的几种页面加载完执行三种方式
jquery加载页面的方法(页面加载完成就执行) 1. $(function(){ $("#a").click(function(){ //adding your code h ...
- Git 个人/团队项目的创建(一步一图)
好吧,不能从简书上直接拷贝过来,所以这里如果有想了解的就直接去我的简书吧. 简书地址
- Xcode7 Xcode6 中添加pch文件
在Xcode7 和 Xcode6 中添加.pch文件是一样的,具体操作图文如下: 第一步:在Xcode的项目里,一般在Supporting Files 文件夹下创建,选中Supporting File ...
- c常用字符串函数
获取字符串长度 : size_t strlen(const char *str); 字符串拷贝函数: 把src中内容拷贝到dest中,它会覆盖原来的内容,它会把src中的\0,没有覆盖内容不变 如果s ...
- 结合实例分析简单工厂模式&工厂方法模式&抽象工厂模式的区别
之前写过一篇关于工厂模式(Factory Pattern)的随笔,里面分析了简单工厂模式,但对于工厂方法和抽象工厂的分析较为简略.这里重新分析分析三者的区别,工厂模式是java设计模式中比较简单的一个 ...