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为 ...
随机推荐
- I/O流任务
一.完成以下链接[https://www.cnblogs.com/zhrb/p/6834084.html]中的任务3.4.5. 3. 字符编码 主要讲解中文乱码问题,FileReader.InputS ...
- Redis的高级应用——数据安全
Redis的数据保存在内存中,速度十分快.这也就意味着,一个恶意破解redis数据库密码的用户,可以在一秒钟进行更多的尝试.如果用户密码级别较低或更换频率过长,就会造成致命的危害. 1.设置用户 Re ...
- 2018牛客多校第三场 C.Shuffle Cards
题意: 给出一段序列,每次将从第p个数开始的s个数移到最前面.求最终的序列是什么. 题解: Splay翻转模板题.存下板子. #include <bits/stdc++.h> using ...
- underscore的bind和bindAll方法
bind方法和bindAll方法都是用来设定函数的this值的,区别是调用方式不同. var xiaoming = { say:function(){ console.log('I am xiaomi ...
- NOIP2010 引水入城 贪心+DFS
我们先把简单的不能搞死,具题意可证:每个蓄水长的管辖区域一定是连续的.证明:既然我们已经能了那么我们就可以说如果这个区间不是连续的那我们取出这个区间中间阻隔开的那一段,那么对于这一整个区间来说水源不可 ...
- 一个 React & Redux的目录树
|-----------------------------------------| | | | React & Redux | | | |------------------------- ...
- 如何使用Eclipse调试framework
1.下载Eclipse EE(下载地址:http://www.eclipse.org/downloads/) 2.下载并安装JDK(下载地址:http://www.oracle.com/technet ...
- Codeforces Round #506 (Div. 3) 题解
Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...
- E. Sonya and Ice Cream(开拓思维)
E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- vsftpd主动模式和被动模式的区别
何为主动模式,何为被动模式 1.ftp采用两个端口控制: 20端口用于数据传输. 21端口用于控制,或指建立TCP连接. 2.主动方式连接过程: [注意]:C表示客户端 S表示服务器端 S端要开启20 ...