Problem Description Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
Input The input contains multiple test cases.
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