博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3230 Travel(dp)
阅读量:6002 次
发布时间:2019-06-20

本文共 2589 字,大约阅读时间需要 8 分钟。

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 33 1 22 3 11 3 22 4 34 3 23 4 20 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;

dp[i][j] 代表第i天在j城市 最多赚了多少钱。

起点在1,所以dp[0][1]=0

然后三重循环dp就好了·

注意赚的钱有可能是负的~

 

1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std;15 #define ll long long16 #define eps 1e-1017 #define MOD 100000000718 #define N 10619 #define inf 1<<2620 int n,m;21 int cost[N][N];22 int _get[N][N];23 int dp[N][N];24 int main()25 {26 while(scanf("%d%d",&n,&m)==2){27 if(n==0 && m==0){28 break;29 }30 for(int i=1;i<=n;i++){31 for(int j=1;j<=n;j++){32 scanf("%d",&cost[i][j]);33 }34 }35 for(int i=1;i<=m;i++){36 for(int j=1;j<=n;j++){37 scanf("%d",&_get[i][j]);38 }39 }40 for(int i=0;i<=m;i++){41 for(int j=0;j<=n;j++){42 dp[i][j]=-inf;43 }44 }45 dp[0][1]=0;//第0天,从城市1开始,值为046 for(int i=1;i<=m;i++){47 for(int j=1;j<=n;j++){48 for(int k=1;k<=n;k++){49 if(_get[j][k]<0)continue;50 dp[i][k]=max(dp[i][k],dp[i-1][j]-cost[j][k]+_get[i][k]);51 }52 }53 } 54 55 int maxxx=-inf;56 for(int i=1;i<=n;i++){57 maxxx=max(maxxx,dp[m][i]);58 }59 printf("%d\n",maxxx);60 }61 return 0;62 }
View Code

 

 

 

转载地址:http://jjdmx.baihongyu.com/

你可能感兴趣的文章
POJ 1703 Find them, Catch them
查看>>
[共通]手机端网页开发问题及解决方法整理
查看>>
HSRP 原理
查看>>
监控队列的脚本
查看>>
我不是九爷 带你了解 CloudStack+XenServer详细部署方案(3):CloudStack管理节点的安装和配置...
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
用户调查报告
查看>>
发布一个php分页类
查看>>
我的友情链接
查看>>
${basePath}
查看>>
linux命令之uniq简单用法
查看>>
Java Socket 网络编程常见异常
查看>>
使用Eclipse调试Java程序的10个技巧
查看>>
Hive分桶表
查看>>
我的友情链接
查看>>
主机屋使用心得
查看>>
编程第11年从为赚钱(薪水)工作转变为“做自己想做的事情”,为兴趣爱好工作、享受人生乐趣...
查看>>
linux vi 保存退出与不保存退出
查看>>
CS5序列号文件,以后就不用担心序列号了
查看>>