UVA 11383 Golden Tiger Claw
2015-07-27 09:54
489 查看
Golden Tiger Claw
Time Limit: 8000msMemory Limit: 131072KB
This problem will be judged on UVA. Original ID: 11383
64-bit integer IO format: %lld Java class name: Main
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) \leq row(i) + col(j) $where is the number assigned to the cell located at ith row and jth column, row(i) is the number assigned to ith row and col(j) is the number assigned to jth column. That is simple isnt it? Well the main part is that you have to minimize $\sum_{1\leq i \leq n}(row(i) + col(i))$.
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.
SAMPLE INPUT
2
1 1
1 1
OUTPUT FOR SAMPLE INPUT
1 1
0 0
2
解题:来自大白书上的题目,KM算法的一个副产品,$L(x) + L(y) \geq w(x,y)$ KM算法结束后,所有顶标之和是最小的.
#include <bits/stdc++.h> using namespace std; const int maxn = 510; const int INF = 0x3f3f3f3f; int W[maxn][maxn],Lx[maxn],Ly[maxn],slack[maxn]; int n,Link[maxn]; bool S[maxn],T[maxn]; bool match(int u){ S[u] = true; for(int v = 0; v < n; ++v){ if(T[v]) continue; int d = Lx[u] + Ly[v] - W[u][v]; if(!d){ T[v] = true; if(Link[v] == -1 || match(Link[v])){ Link[v] = u; return true; } }else if(slack[v] > d) slack[v] = d; } return false; } void update(){ int d = INF; for(int v = 0; v < n; ++v) if(!T[v] && slack[v] < d) d = slack[v]; for(int u = 0; u < n; ++u){ if(S[u]) Lx[u] -= d; if(T[u]) Ly[u] += d; else slack[u] -= d; } } int KM(){ for(int u = 0; u < n; ++u){ Lx[u] = -INF; Ly[u] = 0; Link[u] = -1; for(int v = 0; v < n; ++v) Lx[u] = max(Lx[u],W[u][v]); } for(int u = 0; u < n; ++u){ for(int v = 0; v < n; ++v) slack[v] = INF; while(true){ memset(S,false,sizeof S); memset(T,false,sizeof T); if(match(u)) break; update(); } } int ret = 0; for(int v = 0; v < n; ++v) if(Link[v] > -1) ret += W[Link[v]][v]; return ret; } int main(){ while(~scanf("%d",&n)){ memset(W,0,sizeof W); for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) scanf("%d",W[i] + j); int ret = KM(); for(int i = 0; i < n; ++i) printf("%d%c",Lx[i],i + 1 == n?'\n':' '); for(int i = 0; i < n; ++i) printf("%d%c",Ly[i],i + 1 == n?'\n':' '); printf("%d\n",ret); } return 0; }
View Code
相关文章推荐
- Hugo简介
- HDU 1013 Digital Roots
- item-based algorithm
- Django中的“惰性翻译”方法的相关使用
- 使用Django的模版来配合字符串翻译工作
- 在Django的模型和公用函数中使用惰性翻译对象
- 在Django框架中设置语言偏好的教程
- Greedy --- HNU 13320 Please, go first
- HDU 1076 An Easy Task
- hdu 1533 Going Home (最小费用最大流)
- Google Play 服务提示更新、Google play 闪退等问题
- HDU 2952 Counting Sheep (DFS)
- HDU 5310 Souvenir
- poj2195 Going Home(二分图的最小权匹配)
- hdoj 3715 Go Deeper 【2-sat 判断可行解 + 二分】
- CDOJ 486 Good Morning 傻逼题
- windows环境下搭建go环境+sublime2
- 042.@interface 非正式协议(category)
- 【Go语言】【16】GO语言的并发
- ORA-00932: inconsistent datatypes: expected - got CLOB