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

    Graph.js Graph.js A JavaScript library for rendering a graph of nodes

  2. paip.sql2k,sql2005,sql2008,sql2008 r2,SQL2012以及EXPRESS版本的区别

    paip.sql2k,sql2005,sql2008,sql2008 r2,SQL2012以及EXPRESS版本的区别 作者Attilax ,  EMAIL:1466519819@qq.com  来源 ...

  3. myeclipse集成weblogicserver

    今天为了学一下JMS的东东, 不得不安装个weblogicserver, 下面是详细的安装步骤: 1. 首先去官网下载一个weblogic: 下载地址: http://download.oracle. ...

  4. wso2esb源码编译总结

    最近花了两周的空闲时间帮朋友把wso2esb的4.0.3.4.6.0.4.7.0三个版本从源码编译出来了.以下是大概的一些体会. wso2esb是基于carbon的.carbon是个基于eclipse ...

  5. 创建httpd启动脚本并加入启动列表开机启动

    第一步: cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd   第二步: vim /etc/init.d/httpd 在第一行#!/bin/s ...

  6. photoswipe

    allowUserZoom: 允许用户双击放大/移动方式查看图片. 默认值 = trueautoStartSlideshow: 当PhotoSwipe激活后,自动播放幻灯片. 默认值 = falsea ...

  7. SQL事务机制

    一.事务概念     事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库系统上执行并发操作时事务是作为最小 ...

  8. 利用JQuery实现全选和反选的几种方法

    前面介绍了利用JavaScript实现全选功能,其中也有要注意的几点,现在讲解下在JQuery怎么实现全选和反选,下面提供了两种方法实现. 如图:要实现的效果是点击全选框全部选中,再点击全部不选中 方 ...

  9. JQ动画 show hide

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. java服务器简单实现

    一 HTTP http请求 一般一个http请求包括以下三个部分: 1 请求方法,如get,post 2 请求头 3 实体 一个http请求的实例如下:GET /index.jsp HTTP/1.1H ...