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;

OJ-ID:
poj-3230

author:
Caution_X

date of submission:
20191019

tags:
dp

description modelling:
某人去旅行一趟,输入包含从城市i->j的花费cost[i][j]和第i天待在城市j可以得到的钱w[i][j],求m天后的最大钱数

major steps to solve it:
dp[i][j]:=第i天待在第j个成=城市得到的最大金钱
dp[i][j]=max(dp[i][j],dp[i-1][k]-cost[k][j]+w[i][j]);

AC code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int dp[][],w[][],cost[][];
int main()
{
//freopen("input.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m)&&n&&m) {
memset(dp,-,sizeof(dp));
for(int i=;i<=n;i++) {
for(int j=;j<=n;j++) {
scanf("%d",&cost[i][j]);
}
}
for(int i=;i<=m;i++) {
for(int j=;j<=n;j++) {
scanf("%d",&w[i][j]);
}
}
dp[][]=;
for(int i=;i<=n;i++) dp[][i]= -cost[][i]+w[][i];
for(int i=;i<=m;i++) {
for(int j=;j<=n;j++) {
for(int k=;k<=n;k++) {
dp[i][j]=max(dp[i][j],dp[i-][k]-cost[k][j]+w[i][j]);
}
}
}
int ans=-;
for(int i=;i<=n;i++) {
ans=max(ans,dp[m][i]);
}
printf("%d\n",ans);
}
}

poj-3230 Travel的更多相关文章

  1. poj 3230 Travel(dp)

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

  2. poj 3230(初始化。。动态规划)

    Travel Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4353   Accepted: 1817 Descriptio ...

  3. POJ 3230 【DP】

    题意: 某货旅行,在n个城市呆m天. 给出从第i个城市到第j个城市的路费,或者留在某个城市的生活费. 给出在第i天在第j个城市的收益. 可以在城市之间任意穿梭逗留没有其他特殊要求. 求收益最大是多少. ...

  4. POJ 3230 DP

    f[i][j]=max(f[i][j],f[i-1][k]-a[k][j]+b[i][j]) i->第i天 j-–>到第j个城市 #include <cstdio> #incl ...

  5. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

  6. 15年-ICPC长春-网络赛

    ID name status one word  POJ 5437 Alisha’s Party 赛后AC. 优先队列,模拟.对时间t排序 POJ 5438 Ponds 赛后AC 循环链表 POJ 5 ...

  7. poj 3229 The Best Travel Design ( 图论+状态压缩 )

    The Best Travel Design Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1359   Accepted: ...

  8. POJ 3229:The Best Travel Design

    Description Dou Nai ), and the end of the travel route hours on traveling every day. Input There are ...

  9. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  10. POJ 3422 Kaka's Matrix Travels

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9567   Accepted:  ...

随机推荐

  1. JS字符串替换,将一个字符串中的特定字符串换成其他字符串

  2. LOAD_DLL_DEBUG_EVENT 时读取 DllName

    这句话是说 lpImageName 和 hFile 存在关联(associated),不是一定指向! 继续读后面那句,“这个数字可能为NULL,或者包含着被调试进程空间中的一个字符串地址.这个地址,相 ...

  3. python基础(30):黏包、socket的其他方法

    1. 黏包 1.1 黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) 同时执行多条命令之后,得到的结果很可能只有一部分,在执行其他命令的时候又接 ...

  4. liunx简单命令

    mysql -h主机地址 -u用户名 -p用户密码 --进入数据库1.显示数据库列表. show databases; 2.显示库中的数据表: use mysql: //打开库 show tables ...

  5. CAD打印图纸要怎么操作?简单方法分享给你

    大家日常生活中多多少少的都接触到过CAD文件,CAD图是借助CAD制图软件来进行绘制完成的.唯一的困惑就是CAD图纸的格式大多数均为dwg格式的,查看起来不是那么的方便?所以很多设计师们都会选择将图纸 ...

  6. crm-2

    1.分页 web必备的功能 1)批量制造测试数据 定义一个空列表用于存储 orm对象 ,models.表名(字段=...)创建orm对象append到列表 ,使用bulk_create(对象列表)一次 ...

  7. Java 方法引用_特性

    JAVA8 方法引用:(四种方法引用的使用) 对象引用的特点:不同的对象可以操作同一块的内容:而方法引用就是指为一个方法设置别名,相当于一个方法定义了不同的名字. 引用静态方法: 类名称 :: sta ...

  8. Python目录和文件处理总结

    1.判断目录是否存在.判断文件是否存在.创建目录.重命名目录或文件 import os #获取当前目录路径: E:\Work\Projects\python print(os.getcwd()) #判 ...

  9. sqlserver查询是否阻塞

    查询当前正在执行的语句 SELECT der.[session_id],der.[blocking_session_id], sp.lastwaittype,sp.hostname,sp.progra ...

  10. SQL学习_SQL函数

    常用的 SQL 函数 1. 算术函数 SELECT ABS(-2),运行结果为 2 SELECT MOD(101,3),运行结果 2 SELECT ROUND(37.25,1),运行结果 37.3 2 ...