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

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;
挺简单的,,但是又坑在初始化了,,
题意:题目有点绕,输入n,m n代表城市数,m代表天数
然后是一个 n*n的矩阵 expense[i][j]代表从第i个城市到第j个城市的花费
然后是一个 m*n的矩阵 income[i][j]代表第i天在第j个城市的收入.
分析:dp[i][j]代表第i天在第j个城市前i天能够获得的最大income(income可能为负)
那么 dp[i][j] = max(dp[i][j],dp[i-1][k]-express[k][i]+income[i][j])
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N=;
int express[N][N];
int income[N][N];
int dp[N][N];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF,n+m){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&express[i][j]);
}
}
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
scanf("%d",&income[i][j]);
}
}
for(int i=;i<=n;++i)
dp[][i]=-;
dp[][]=;///初始化第0天在第1个城市为0
for(int i=;i<=m;i++){ ///枚举天数
for(int j=;j<=n;j++){ ///枚举第i天
dp[i][j]=dp[i-][]+income[i][j]-express[][j];
for(int k=;k<=n;k++){ ///枚举i-1天
dp[i][j]=max(dp[i][j],dp[i-][k]-express[k][j]+income[i][j]);
}
}
}
int ans = -;
for(int i=;i<=n;i++){
ans = max(ans,dp[m][i]);
}
printf("%d\n",ans);
}
return ;
}

poj 3230(初始化。。动态规划)的更多相关文章

  1. poj 3783 Balls 动态规划 100层楼投鸡蛋问题

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...

  2. poj 2229 一道动态规划思维题

    http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...

  3. [POJ 2063] Investment (动态规划)

    题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...

  4. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  5. POJ 1088 滑雪 -- 动态规划

    题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  6. poj 1159 Palindrome - 动态规划

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...

  7. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  8. poj 3230 Travel(dp)

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

  9. poj 1837 Balance 动态规划 (经典好题,很锻炼思维)

    题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...

随机推荐

  1. 【工作感悟】——揭开“PM”的面纱

    [前言] 上次跟大家分享了面试的故事,小编觉得效果还不错,因此小编决定把工作感悟系列写下去,不过时间就不好说了.希望小伙伴们在评论列表中发表自己的看法和观点,积极参与啦~~ [背景] 初来Y公司,作为 ...

  2. IO调度

    互联网公司不关注真实的文件系统,他们关注VFS层,关注block层,关注IO的管控. queue->make_request_fn ( blk_queue_bio ),其中blk_queue_b ...

  3. sessionStorage & URL Origin

    sessionStorage & URL Origin same origin https://developer.mozilla.org/en-US/docs/Web/API/Window/ ...

  4. doget,doPost在底层走的是service

    doget,doPost在底层走的是service 因为在源码上 先执行service方法 然后再调用doget,doPost方法

  5. ES 1.7安装ik分词elasticsearch-analysis-ik-1.2.5

    IK简介 https://www.cnblogs.com/yjf512/p/4789239.html https://www.cnblogs.com/xing901022/p/5910139.html ...

  6. [Leetcode] unique paths 独特路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  7. MySQL之SELECT 语句详解

    本文参考实验楼的SELECT 语句详解结合自己操作部分而写成. 注意:大多数系统中,SQL语句都是不区分大小写的,但是出于严谨和便于区分保留字和变量名,在书写的时,保留字应大写,而变量名应小写.所谓的 ...

  8. BZOJ4008 [HNOI2015]亚瑟王 【概率dp】

    题目链接 BZOJ4008 题解 要求所有牌造成伤害的期望,就是求每一张牌发动的概率\(g[i]\) 我们发现一张牌能否发动,还与其前面的牌是否发动有关 那我们设\(f[i][j]\)表示前\(i\) ...

  9. BZOJ1407 [Noi2002]Savage 【扩展欧几里得】

    题目链接 BZOJ1407 题解 枚举\(m\)用扩欧判即可 #include<algorithm> #include<iostream> #include<cstrin ...

  10. npm错误总结

    You cannot publish over the previously published version 1.0.1." : xxx 发布时一定要修改package.json的版本号 ...