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

    (referrence: GeekforGeeks) Bucket sort is mainly useful when input is uniformly distributed over a r ...

  2. 了解SQL注入攻击

    SQL注入:利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,这是SQL注入的标准释义. 随着B/S模式被广泛的应用,用这种模式编写应用程序的程序员也越来越多,但由于开发人员的水 ...

  3. linux内存操作----kernel 3.5.X copy_from_user()和copy_to_user()

    前面的一篇文章中简单的描写叙述了一下内存映射的内容,http://blog.csdn.net/codectq/article/details/25658813,这篇文章作为用户把内存规划好之后,在用户 ...

  4. 在spring+hibernaet+mysql事务处理中遇到的一些坑

    spring的事务处理本来就是依赖于底层的实现,比如hibernate及数据库本身. 所以,当使用mysql数据库时,首先要确定的是,所操作的对象表是innodb格式的. 1. read-only方法 ...

  5. SQL语言学习-数据操纵语言

    一般而言,数据库中数据的生命周期包括数据插入以及更新.数据删除3个阶段.首先需要用户或者系统将数据插入表.然后,对数据的使用,包括数据的检索以及数据的更新.最后,如果数据已经没有使用价值,则将数据删除 ...

  6. achartengine andorid图像引擎入门

    最近在帮机械学院开发一个app 用了第三方的图像引擎——achartengine功能还算强大(虽然相比于Html那些第三方图像引擎还是差点不过也够用了) 入门: 参考http://blog.csdn. ...

  7. iOS框架介绍

    iOS框架介绍      Cocoa Touch   GameKit  实现对游戏中心的支持,让用户能够在线共享他们的游戏相关的信息  iOS设备之间蓝牙数据传输   从iOS7开始过期   局域网游 ...

  8. su普通用户切换root用户失败

    http://blog.itpub.net/26432034/viewspace-1688391/ http://blog.csdn.net/zhangdaiscott/article/details ...

  9. <转>golang 并发性能数据

    1.管道chan吞吐极限10,000,000,单次Put,Get耗时大约100ns/op,无论是采用单Go程,还是多Go程并发(并发数:100, 10000, 100000),耗时均没有变化,Go内核 ...

  10. C++程序设计实践指导1.5求两个整数集合并集改写要求实现

    改写要求1:改写为单链表结构可以对任意长度整数集合求并集 #include <cstdlib> #include <iostream> using namespace std; ...