poj-3230 Travel
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;
OJ-ID:
poj-3230
author:
Caution_X
date of submission:
20191019
tags:
dp
description modelling:
某人去旅行一趟,输入包含从城市i->j的花费cost[i][j]和第i天待在城市j可以得到的钱w[i][j],求m天后的最大钱数
major steps to solve it:
dp[i][j]:=第i天待在第j个成=城市得到的最大金钱
dp[i][j]=max(dp[i][j],dp[i-1][k]-cost[k][j]+w[i][j]);
AC code:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int dp[][],w[][],cost[][];
int main()
{
//freopen("input.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m)&&n&&m) {
memset(dp,-,sizeof(dp));
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",&w[i][j]);
}
}
dp[][]=;
for(int i=;i<=n;i++) dp[][i]= -cost[][i]+w[][i];
for(int i=;i<=m;i++) {
for(int j=;j<=n;j++) {
for(int k=;k<=n;k++) {
dp[i][j]=max(dp[i][j],dp[i-][k]-cost[k][j]+w[i][j]);
}
}
}
int ans=-;
for(int i=;i<=n;i++) {
ans=max(ans,dp[m][i]);
}
printf("%d\n",ans);
}
}
poj-3230 Travel的更多相关文章
- poj 3230 Travel(dp)
Description One traveler travels among cities. He has to pay for this while he can get some incomes. ...
- poj 3230(初始化。。动态规划)
Travel Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4353 Accepted: 1817 Descriptio ...
- POJ 3230 【DP】
题意: 某货旅行,在n个城市呆m天. 给出从第i个城市到第j个城市的路费,或者留在某个城市的生活费. 给出在第i天在第j个城市的收益. 可以在城市之间任意穿梭逗留没有其他特殊要求. 求收益最大是多少. ...
- POJ 3230 DP
f[i][j]=max(f[i][j],f[i-1][k]-a[k][j]+b[i][j]) i->第i天 j-–>到第j个城市 #include <cstdio> #incl ...
- {POJ}{动态规划}{题目列表}
动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...
- 15年-ICPC长春-网络赛
ID name status one word POJ 5437 Alisha’s Party 赛后AC. 优先队列,模拟.对时间t排序 POJ 5438 Ponds 赛后AC 循环链表 POJ 5 ...
- poj 3229 The Best Travel Design ( 图论+状态压缩 )
The Best Travel Design Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1359 Accepted: ...
- POJ 3229:The Best Travel Design
Description Dou Nai ), and the end of the travel route hours on traveling every day. Input There are ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 3422 Kaka's Matrix Travels
Kaka's Matrix Travels Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9567 Accepted: ...
随机推荐
- Pull Request找原作者做review
最近对代码库进行了一次全局替换,改了150个左右的文件,发了PR后需要找原作者确认一下,于是写了个工具做git blame,地址在这里 写完发现一个问题,如果只是新增一个函数,没有调用,是应该不用找原 ...
- MySQL 中的字符串类型
字符类型包括: CHAR VARCHAR BINARY VARBINARY BLOB TEXT ENUM SET CHAR 与 VARCHAR CHAR(m) m 取值范围 0-255.列宽固定,存储 ...
- VS2017安装使用Easyx时出现的问题及解决方法
EasyX 是针对 Visual C++ 的绘图库,在初学 C 语言实现图形和游戏编程.图形学.分形学等需要绘图实践的领域有一定应用. EasyX 库在 Visual C++ 中模拟了 Turbo C ...
- python自动登录代码
公司有很多管理平台,账号有禁用机制,每个月至少登录一次,否则禁用.导致有时候想登录某个平台的时候,发现账号已经被禁用了,还得走流程解禁.因此用python实现了一下自动登录,每天定时任务运行一次.ps ...
- golang中type常用用法
golang中,type是非常重要的关键字,一般常见用法就是定义结构,接口等,但是type还有很多其它的用法,在学习中遇到了以下几种,这点简单总结记录下 定义结构 type Person struct ...
- 为什么要学 Python? python该怎么学
很多童鞋对为什么学习Python感到迷茫,小编来跟大家说说学习Python的10个理由,希望可以帮助到大家!!! 摘要: 看完这十个理由,我决定买本python从入门到精通! 如果你定期关注现今的科技 ...
- 性能篇系列—stream详解
Stream API Java 8集合中的Stream相当于高级版的Iterator Stream API通过Lambda表达式对集合进行各种非常便利高效的聚合操作,或者大批量数据操作 Stream的 ...
- JavaWeb之(1)Tomcat安装及项目的发布方法
Tomcat安装及项目的发布方法 Tomcat安装 1.直接解压,然后找到bin/startup.bat 2.双击,如果出现命令行界面且最后一句为"信息: Server startup in ...
- 1041. Robot Bounded In Circle
本题题意: 一开始一个机器人站在了(0,0)上,面朝的方向是北,收到三个序列G,L,R. G:直走 L:向左转 R:向右转 按序执行,永远重复. 返回TRUE,如果处在一个圈. 第一个卡住的点: 1. ...
- SSRS 报表开发过程中,除数为0的处理
这里仅供记录,方法并非原创 在SSRS报表开发过程中,我们经常会遇到除数为0的计算 一般来说,我们都是通过IIF来进行处理,比如: =IIF(B=0,0,A/B) 但实际效果,则是,如果B=0的时候, ...