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<.

Output

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

Sample Input


Sample Output


Hint

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

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

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

然后三重循环dp就好了·

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

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1<<26
int n,m;
int cost[N][N];
int _get[N][N];
int dp[N][N];
int main()
{
while(scanf("%d%d",&n,&m)==){
if(n== && m==){
break;
}
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",&_get[i][j]);
}
}
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
dp[i][j]=-inf;
}
}
dp[][]=;//第0天,从城市1开始,值为0
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
for(int k=;k<=n;k++){
if(_get[j][k]<)continue;
dp[i][k]=max(dp[i][k],dp[i-][j]-cost[j][k]+_get[i][k]);
}
}
} int maxxx=-inf;
for(int i=;i<=n;i++){
maxxx=max(maxxx,dp[m][i]);
}
printf("%d\n",maxxx);
}
return ;
}

poj 3230 Travel(dp)的更多相关文章

  1. POJ 3034 Whac-a-Mole(DP)

    题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在( ...

  2. poj 2229 Sumsets(dp)

    Sumsets Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 400000/200000K (Java/Other) Total Sub ...

  3. poj -2229 Sumsets (dp)

    http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...

  4. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  5. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  6. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  7. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  8. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  9. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

随机推荐

  1. 2014.8.4我出的模拟赛【你的名字叫czy是吧】

    你的名字叫czy是吧 (mynameisczy.pas/.c/.cpp) 尽管czy放了那么多只NTR酋长,也没能拦住黄巨大.黄巨大和czy相遇了…… “你的名字叫czy是吧” “……” “我们来单挑 ...

  2. Add Digits 解答

    Question Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  3. iOS学习之iOS沙盒(sandbox)机制和文件操作(一)

    1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...

  4. 使用 AngularJS 和 ReactJS 的经验

    1. React 福音 当我们的团队开始寻找一个合适的前端框架的时候,我们考虑了许多选择,最后留下两个选项 —— Angular 和 React. Angular 是目前为止最成熟的方案:它拥有一个庞 ...

  5. Qt 信号与槽

    Qt信号与槽的理解 信号和槽机制是 QT 的核心机制,要精通 QT 编程就必须对信号和槽有所了解.信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重 ...

  6. 配置基于NotePad++工具下的C#开发环境

    1.打开NotePad++,打开Notepad++的插件(plugins)菜单-->Plugin Manager-->Show Plugin Manager-->勾选NppExec- ...

  7. Ext布局

    Ext主要包括11种标准布局方式:Auto(自动布局).CheckboxGroup(复选框组布局).Fit(自适应布局).Column(列布局).Accordion(折叠布局).Table(表格布局) ...

  8. 追加addclass和removeclass

    //addclass             Base.prototype.addclass=function(classname){                 for(var i=0;i< ...

  9. Letter of inquiry about employment possibilities, e-mail version

    Subject: (logical to recipient!) Inquiry about software engineering position after completion of M.S ...

  10. div背景等比例缩小

    background: url("http://www.asdear.com/Content/loginPage/newimages/nchina.png") 50% 0px no ...