Hi, I'm so interested for knowing some hints for this problem : http://uva.onlinejudge.org/contests/309-2f118707/a.html
any help will be appreciated.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Hi, I'm so interested for knowing some hints for this problem : http://uva.onlinejudge.org/contests/309-2f118707/a.html
any help will be appreciated.
Name |
---|
Does this problem require a matching(or something like this) approach to solve ?!
I was thinking about this problem too. It seems that it can be solved as with matching, as with min-cost. But I still can't solve it :(
I think it can be solved using minimum cost flows. However, time limit is rather tight, so maybe my solution needs some optimizations. If you provide a link where I can test my solution I will check it and tell you more.
Inputs/Outputs of contest have been uploaded here : http://acm-icpc.coe.psu.ac.th/contest-problem/
can you say briefly about your idea ? how you construct the graph/network ?
This link isn't working for me right now.
Briefly, as usual we have vertices for each column and row and edges between rows and columns correspond to elements of matrix. Edges are directed, if it's 0 initially then it goes from row to column, and if it's 1 then it goes in opposite direction, all these edges have capacity and cost of 1. We add edges between source, sink and all these vertices too of course, based on how many 1s will be in each column and row at the end. I don't want to write further details, partly because I'm not sure if this solution is right, partly because I think there is simpler solution, partly because I want to sleep :)
On this page you will find the solution proposed by the creator of the problem and test cases
http://acm-icpc.coe.psu.ac.th/contest-problem/
coding this problem is difficult
I got my solution for this problem accepted during the UVA contest, so I can tell you what I did.
I tried every possible number of 1s for the rows (let this number be x). Once x is fixed, y is uniquely determined: y = (x * number_of_rows) / number_of_columns (and y must be an integer).
Then, in order to compute the minimum number of moves for the pair (x,y) I used min-cost max-flow on the following graph (similar to what mmaxio described):
all the rows are vertices on the left side
all the columns are vertices on the right side
we add an extra vertex S and edges from S to each row, of capacity x and cost 0
we add an extra vertex T and edges from each column to T, of capacity y and cost 0
each edge between row i and column j has capacity 1 and:
Then you just run a min-cost max-flow algorithm and the result is the starting cost + the minimum cost obtained by the min-cost max-flow algorithm.
However, the min-cost max-flow algorithm needs to be optimized in order to get it to run within the time limit. I will post below the optimizations that I used (I also explained them for a different problem from Codechef's November 2012 contest):
1) I used the Bellman-Ford-Moore (BFM) shortest path algorithm -- this is in theory O(N^3), but it always runs much faster than that : it uses a simple queue in which a node is inserted whenever the optimal distance to it is updated.
2) I also used the "parent checking" heuristic for BFM — i.e. when you extract a node from the queue, don't do anything with it if its parent is already in the queue again (this can be extended to multiple levels — e.g. the parent's parent, etc.).
3) There is no need to update the flow on only one path at a time — just like in the case of Hopcroft-Karp's maximum matching algorithm , we can update the flow on multiple vertex-disjoint paths after running the shortest path algorithm -- this reduces the number of times the shortest path algorithm is run, which is in fact the bottleneck in this solution.
4) Finally, after running the shortest path algorithm, visit every column and add to a sum the minimum cost of reaching that column times the number of units of flow that column still needs (i.e. the capacity of the edge from the column to T minus the flow on that edge). If you add this sum to the current minimum cost you will get the minimum possible cost you may still get. If this minimum possible cost is >= than the minimum cost found so far then there is no need to continue with the algorithm (it means that the current pair (x,y) which is tested cannot be the optimal one).
thanks a lot , I fully understand your nice idea.
It's very detailed. Thank you very much!
Can you give your code of min-cost-max-flow with these optimizations?