poj 3230 Travel(dp)

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 3
3 1 2
2 3 1
1 3 2

2 4 3
4 3 2
3 4 2

0 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;

dp[i][j] 代表第i天在j城市 最多赚了多少钱。

起点在1,所以dp[0][1]=0

然后三重循环dp就好了·

注意赚的钱有可能是负的~

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 using namespace std;
15 #define ll long long
16 #define eps 1e-10
17 #define MOD 1000000007
18 #define N 106
19 #define inf 1<<26
20 int n,m;
21 int cost[N][N];
22 int _get[N][N];
23 int dp[N][N];
24 int main()
25 {
26     while(scanf("%d%d",&n,&m)==2){
27         if(n==0 && m==0){
28             break;
29         }
30         for(int i=1;i<=n;i++){
31             for(int j=1;j<=n;j++){
32                 scanf("%d",&cost[i][j]);
33             }
34         }
35         for(int i=1;i<=m;i++){
36             for(int j=1;j<=n;j++){
37                 scanf("%d",&_get[i][j]);
38             }
39         }
40         for(int i=0;i<=m;i++){
41             for(int j=0;j<=n;j++){
42                 dp[i][j]=-inf;
43             }
44         }
45         dp[0][1]=0;//第0天,从城市1开始,值为0
46         for(int i=1;i<=m;i++){
47             for(int j=1;j<=n;j++){
48                 for(int k=1;k<=n;k++){
49                     if(_get[j][k]<0)continue;
50                     dp[i][k]=max(dp[i][k],dp[i-1][j]-cost[j][k]+_get[i][k]);
51                 }
52             }
53         }
54
55         int maxxx=-inf;
56         for(int i=1;i<=n;i++){
57             maxxx=max(maxxx,dp[m][i]);
58         }
59         printf("%d\n",maxxx);
60     }
61     return 0;
62 }

时间: 2024-08-27 20:28:47

poj 3230 Travel(dp)的相关文章

POJ 3034 Whac-a-Mole(DP)

题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在(x1,y1),那下一秒直线移动到的整数点(x2,y2)与这个点的距离小于等于d,并且当锤子移动(x2,y2)这个点时,所有在两点的直线上的整点数都可以打到.例如(0,0)移动到(0,3).如果(0,1),(0,2)有老鼠出现就会被打到.求能够打的最多老鼠. 思路 : Dp[i][j][k]代表点(i,j)

poj - 1722 - SUBTRACT(dp)

题意:一个长为 N (1 <= N <= 100)的序列(1 <= ai <= 100),一次操作为删除 a[i] 和 a[i + 1],然后将它们的差(a[i] - a[i + 1])放入该位置,问 N - 1 次操作后得到 T (-10000 <= T <= 10000)的操作顺序是什么? 题目链接:http://poj.org/problem?id=1722 -->>每次操作相当于给 a[i] 和 a[i + 1] 加括号做减法,那么把所有的括号去掉后

poj 1745 Divisibility(DP)

最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 给你一个m×n的整数矩阵,在上面找一个x×y的子矩阵,使子矩阵中所有元素的和最大. Input 输入数据的第一行为一个正整数T,表示有T组测试数据.每一组测试数据的第一行为四个正整数m,n,x,y(0<m,n<1000 AND 0<x<=m AND 0<y

poj - 1088 - 滑雪(dp)

题意:一个R * C的矩阵(1 <= R,C <= 100),元素代表该点的高度h(0<=h<=10000),从任意点出发,每次只能走上.下.左.右,且将要到的高度要比原高度小,求最长路. 题目链接:http://poj.org/problem?id=1088 -->>设dp[i][j]表示从ij位置出发的最长路,则状态转移方程为: dp[x][y] = max(dp[x][y], Dp(nNewX, nNewY) + 1); 时间复杂度:O(R * C) #inclu

POJ 1440-Varacious Steve(DP)

题目大意:Steve和Digit轮流取甜甜圈,有n个,每次不能取超过m个(1<=m<=n<=100),他们游戏的规则是,Steve先取,每个人取了之后甜甜圈就放在自己那里,当某个人取完最后一个以后,他吃掉自己那里的甜甜圈并把对手的所有甜甜圈拿出来,重复这样的过程,当一个人吃掉那对手的出来后,那一轮游戏是对手先手,求Steve能吃到的最多的甜甜圈. 用d[i][j][u][0]表示目前总共有i个甜甜圈,其中j个还未被取,u个在Steve处,且下一步到Steve走时,Steve能吃到的最多的

POJ 2192 Zipper (dp)

链接: http://poj.org/problem?id=2192 题意:就是给定三个字符串A,B,C:判断C能否由AB中的字符组成,同时这个组合后的字符顺序必须是A,B中原来的顺序,不能逆序:例如:A:mnl,B:xyz:如果C为mnxylz,就符合题意:如果C为mxnzly,就不符合题意,原因是z与y顺序不是B中顺序. DP求解:定义dp[i][j]表示A中前i个字符与B中前j个字符是否能组成C中的前 (i+j) 个字符,如果能,标记1,如果不能,标记0: 有了这个定义,我们就可以找出状态

POJ 2948 Martian Mining(DP)

题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿. 思路 :记忆化搜索.也可以用递推. //2948 #include <stdio.h> #include <string.h> #include <iostream> using namespace std ; int yeye[510][510] ,blog[510]

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

【POJ 2029】 Get Many Persimmon Trees(DP)

[POJ 2029] Get Many Persimmon Trees(DP) Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4024   Accepted: 2628 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18