zoukankan      html  css  js  c++  java
  • poj 3230 Travel(dp)

    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;

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

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

    然后三重循环dp就好了·

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

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 using namespace std;
    15 #define ll long long
    16 #define eps 1e-10
    17 #define MOD 1000000007
    18 #define N 106
    19 #define inf 1<<26
    20 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开始,值为0
    46         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
    ",maxxx);
    60     }
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    三道 华为 面试题
    百度面试题01——50个阶梯,你一次可以上一阶或两阶,走上去,共有多少种走法?
    一首笔试题 C实现
    图像缩放算法及速度优化——(一)最近邻插值
    一个简单的二叉树排序算法
    OS的四大特征
    OS的目的和功能
    桶排序
    快速排序
    OS的运行机制
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5143327.html
Copyright © 2011-2022 走看看