链接:Tian Ji -- The Horse Racing | JXNUOJ
Here is a famous story in Chinese history.
"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."
"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."
"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."
"Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."
"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"
田忌赛马。田忌和国王各有三匹马,三匹马的耐力、优劣皆不同。规则是进行三轮比赛:每匹马只能比赛一次。单句获胜者从输的那方得到200元。因为国王权力最大,所以在三种层次的马中,国王的马均比田忌的马好,那么结果可能是国王从田忌那里得到600元。田忌很苦恼,但这时,他遇到了孙膑,孙膑是后来史书上常见的兵法家。田忌听取了孙膑的小计谋,他从家里拿出200元。方法很简单,用田忌的劣马去和国王最好的马比赛,这只会输掉这只会输掉这一轮,然后用最好的马打败国王中马,中等的马打败国王的劣马。这么简单的一个计谋,你是怎样看他的?
Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...
However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.
In this problem, you are asked to write a program to solve this special case of matching problem.
现在,田忌的策略被认为是在图里寻找完美匹配的方法。画田忌的马在一边,国王的马在另一边。只要是田忌的马能打包国王的马,我们在这两只马之间连条线,意思是我们希望这一配对(这两只马比赛)是成立的。接着,尽可能多赢几场比赛仅仅是找到最大匹配(田忌的马必赢)。如果有关系,问题变得复杂了。他需要给所有可能的边指定权重1 -1 0,找到最大加权完美匹配。然而,赛马问题是二部分配中比较特别的一种。这个图形由马的速度决定--速度越高的顶点总是比最低的快。这种情况下,加权二部匹配算法就太先进了。
你需要写一个算法去解决这个特殊问题。
输入:
The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.
输入最多50个测试数据。每组测试数据,第一行输入n,是每边马的数量,接下来一行的n个数字表示田忌的马的速度。第三行的表示国王的马的速度。最后一组输入后,输入0表示结束输入。
输出:
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
对于每组数据,输入单独一行,一个数表示田忌将会赢得最多的钱。以银元为单位。