poj3230,

## Problem:

Travel

 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2951 Accepted: 1240

Description

One traveler travels among cities. He has to pay for this while he can get some incomes.

Now there are n cities, and the traveler has m days for traveling. Everyday he may go to another city or stay there and pay some money. When he come to a city ,he can get some money. Even when he stays in the city, he can also get the next day's income. All the incomes may change everyday. The traveler always starts from city 1.

Now is your turn to find the best way for traveling to maximize the total income.

Input

There are multiple cases.

The first line of one case is two positive integers, n and m .n is the number of cities, and m is the number of traveling days. There follows n lines, one line n integers. The j integer in the i line is the expense of traveling from city i to city j. If i equals to j it means the expense of staying in the city.

After an empty line there are m lines, one line has n integers. The j integer in the i line means the income from city j in the i day.

The input is finished with two zeros.
n,m<100.

Output
You must print one line for each case. It is the max income.

Sample Input
`3 33 1 22 3 11 3 22 4 34 3 23 4 20 0`

Sample Output
`8`

Hint
In the Sample, the traveler can first go to city 2, then city 1, and finish his travel in city 1. The total income is:
-1+4-2+4-1+4=8;

Source

## Solution:

Initially, I use the dfs to implement every possible way, but it will get a TLE. (No doubt).

Then, DP is a good choice to implement it.

Basically, it's a straight forward dynamic programming problem.

Initialization:

dp[0][1] = 0; Day 0, City 1;

dp[1][k] = dp[0][1] + income[1][k] - cost[1][k]; 1<=k <=n;

The state formula is like:

ith day, city j:

dp[i][j]  =  max{ dp[i-1][j] + income[i][j] - cost[j][j],  (1<=k<=n) dp[i-1][k] + income[i][j] - cost[k][j]};

## Source Code:

//Fri Apr 9 00:58:12 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int maxincome = -1;
int n, m;

void Solve(vector<vector<int> > cost, vector<vector<int> > income, int city,
int currentDay, int day, int maximum)
{
maximum += income[city][currentDay - 1];
if (day == m)
{
maxincome = max(maxincome, maximum);
return;
}
for (int i = 0; i < n; i++)
{
if (i == city)
Solve(cost, income, city, currentDay + 1, day + 1, maximum
- cost[city][city]);
else
Solve(cost, income, i, 1, day + 1, maximum - cost[city][i]);
}
}

int main(int argc, char* argv[])
{
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
while (cin >> n >> m && (n || m))
{
vector<vector<int> > cost(100, vector<int> (100, 0));
vector<vector<int> > income(100, vector<int> (100, 0));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
cin >> cost[i][j];
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
cin >> income[i][j];
}
// Solve(cost, income, 1, 1, 1, 0);

vector<vector<int> > dp(100, vector<int> (100, 0));

for (int i = 1; i <= n; i++)
{
dp[1][i] = dp[0][1] - cost[1][i] + income[1][i];
}

for (int i = 2; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
dp[i][j] = dp[i - 1][j] + income[i][j] - cost[j][j];
for (int k = 1; k <= n; k++)
{
dp[i][j] = max(dp[i][j], dp[i - 1][k] + income[i][j]
- cost[k][j]);
}
maxincome = max(maxincome, dp[m][j]);
}
}
cout << maxincome << endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}
[/sourcecode]