poj 3230(初始化。。动态规划)
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
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
-1+4-2+4-1+4=8;
然后是一个 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(初始化。。动态规划)的更多相关文章
- poj 3783 Balls 动态规划 100层楼投鸡蛋问题
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...
- poj 2229 一道动态规划思维题
http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...
- [POJ 2063] Investment (动态规划)
题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...
- [POJ 2923] Relocation (动态规划 状态压缩)
题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...
- POJ 1088 滑雪 -- 动态规划
题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- poj 1159 Palindrome - 动态规划
A palindrome is a symmetrical string, that is, a string read identically from left to right as well ...
- poj 2385【动态规划】
poj 2385 Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14007 Accepte ...
- poj 3230 Travel(dp)
Description One traveler travels among cities. He has to pay for this while he can get some incomes. ...
- poj 1837 Balance 动态规划 (经典好题,很锻炼思维)
题目大意:给你一个天平,并给出m个刻度,n个砝码,刻度的绝对值代表距离平衡点的位置,并给出每个砝码的重量.达到平衡状态的方法有几种. 题目思路:首先我们先要明确dp数组的作用,dp[i][j]中,i为 ...
随机推荐
- const在c/c++中的区别
#include <iostream> using namespace std; int main() { ; ; }; ; i < sizeof array / sizeof *a ...
- 下拉框select chosen被遮盖
最简单的就是让容器高度大点. 用js调整也行. 为什么z-index不管事,看下面... 浏览器支持 所有主流浏览器都支持 z-index 属性. 注释:任何的版本的 Internet Explore ...
- EXEL文件转成简书MD表格
EXEL文件转成简书MD表格 0.1.3 mac: https://github.com/fanfeilong/exceltk/blob/master/pub/exceltk.0.1.3.pkg wi ...
- Log4Net讲解
声明:本文内容主要译自Nauman Leghari的Using log4net,亦加入了个人的一点心得(节3.1.4). 1 简介 1.1 Log4net的优点: ...
- perf 对两个map是否重叠的判断,以及函数map_groups__fixup_overlappings代码逻辑
该标题可以抽象出来的问题是:两个前开后闭的区间 rangeA 和 rangeB,如何判断这两个区间是否重叠.这个问题在内核中非常重要,虚拟地址空间的划分需要它,perf中map_group的构建也需要 ...
- mysql类型与java类型对应异常场景
MySQL的bigint unsigned类型,对应java的BigInteger类型,在基于mybatis框架时,如果我将bigint unsigned类型的字段的返回放在一个map的返回类型中时. ...
- 批处理之SET命令
除了 下面分别介绍: 表示第二个字符到倒数第三个字符的值
- Windows 64下elasticsearch-1.7.1集群 安装、启动、停止
elasticsearch-1.7.1 (es Windows 64) 安装.启动.停止的详细记录 https://blog.csdn.net/qq_27093465/article/details/ ...
- Mapreduce简要原理与实践
探索Mapreduce简要原理与实践 目录-探索mapreduce 1.Mapreduce的模型简介与特性?Yarn的作用? 2.mapreduce的工作原理是怎样的? 3.配置Yarn与Mapred ...
- 【bzoj2809】[Apio2012]dispatching 贪心+可并堆
题目描述 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增 ...