Than n lines,each line include n positive integers.(<100)
Output For each test case output the maximal values yifenfei can get.
Sample Input
2
10 3
5 10
3
10 3 3
2 5 3
6 7 10
5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Sample Output
28
46
80
Author yifenfei
Source ZJFC 2009-3 Programming Contest
Recommend yifenfei | We have carefully selected several similar problems for you: 3376 2448 3395 3491 3313
题意:给出一个n*n的矩阵,每个点上都有一个值,现在从左上角沿着一条路径走到右下脚(只能向右或者向下),然后再从右下角回到左上角(只能向左或者向上),在这个过程中每个点只允许走一次,问路径上的权值之和最大为多少?
?
思路:这里用到费用流求解,首先添加一个超级源点s=0和超级汇点t=n*n+1,然后对每个点拆点, i 向 i` 连边,容量为1,花费为该点的权值mp[i][j],然后s与 1` 连边,容量为2,花费为0,n*n向t连边,容量为2,花费为0,最后矩阵中的点之间连边,容量为1,花费为0。最后答案为cost+mp[1][1]+mp[n][n]。注意数组的大小。
代码:
?
#include
#include
#include
#include
#include
#include
#include
?