HDU 4862
Jump
最小费用最大流。 很经典的网络流建图。
Jump
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 189 Accepted Submission(s): 63
Problem Description
There are n*m grids, each grid contains a number, ranging from 0-9. Your initial energy is zero. You can play up to K times the game, every time you can choose any one of the grid as a starting point (but not traveled before) then you can choose a grid on the right or below the current grid to jump, but it has not traveled before. Every time you can jump as many times as you want, as long as you do not violate rules. If you are from (x1, y1) to (x2, y2), then you consume |x1-x2|+|y1-y2|-1 energies. Energy can be negative. However, in a jump, if you start position and end position has same numbers S, then you can increase the energy value by S. Give me the maximum energy you can get. Notice that you have to go each grid exactly once and you don’t have to play exactly K times.
Input
The first line is an integer T, stands for the number of the text cases. Then T cases followed and each case begin with three numbers N, M and K. Means there are N rows and M columns, you have K times to play. Then N lines follow, each line is a string which is made up by M numbers. The grids only contain numbers from 0 to 9. (T<=100, N<=10,M<=10,K<=100)
Output
Each case, The first you should output “Case x : ”,(x starting at 1),then output The maximum number of energy value you can get. If you can’t reach every grid in no more than K times, just output -1.
Sample Input
5 1 5 1 91929 1 5 2 91929 1 5 3 91929 3 3 3 333 333 333 3 3 2 333 333 333
Sample Output
Case 1 : 0 Case 2 : 15 Case 3 : 16 Case 4 : 18 Case 5 : -1
Author
FZU
Source
2014 Multi-University Training Contest 1
建图方法: 左右各n*m个点。加一个源点和一个汇点。 源点向左侧的每一个点连一条流量为1,费用为0的边。 右侧点连一条流量为1,费用为0的边到汇点。 如果左侧点的点,可以转移的话,对应连一条左侧点到右侧点的边,一步到达。 费用是消耗的费用-获得的费用。 再加一个点,起点到这个点连流量为1,费用为0的边。这个点向右侧的点连一条流量为1,费用为0的边。。 如果非满流,则不满足。 否则最小费用相反数就是最大收益。
1 | /* *************** |