[水+dp] poj 3230 Travel

题意:

给你n个城市m天。

每个城市有到达的花费 c[i][j] 代表城市i到城市j的花费 本身到本身也有花费 就相当于住宿费吧。

接着每天在每个城市都能赚钱  p[m][n] 代表每天每个城市赚的前。

问m天后最多能赚多少钱。

思路:

比较水的dp吧。

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

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

然后三重循环dp就好了·

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

然后输入的n和m别反了。

代码:

#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"queue"
#include"algorithm"
#include"iostream"
#include"map"
#include"stack"
#include"vector"
#define ll __int64
#define inf -999999999999999999LL
using namespace std;
ll c[1234][1234],p[1234][1234];
ll dp[1234][1234];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m),(n+m))
    {
        if(n==0 || m==0)
        {
            puts("0");
            continue;
        }
        for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) scanf("%I64d",&c[i][j]);
        for(int i=1; i<=m; i++) for(int j=1; j<=n; j++) scanf("%I64d",&p[i][j]);
        for(int i=0; i<=m; i++) for(int j=1; j<=n; j++) dp[i][j]=inf;
        dp[0][1]=0;
        for(int i=1; i<=m; i++)
        {
            for(int j=1; j<=n; j++)
            {
                for(int k=1; k<=n; k++)
                {
                    if(p[i][k]<0) continue;
                    dp[i][k]=max(dp[i][k],dp[i-1][j]+p[i][k]-c[j][k]);
                }
            }
        }
        ll ans=inf;
        for(int i=1; i<=n; i++) ans=max(ans,dp[m][i]);
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-08-02 01:29:36

[水+dp] 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. 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

poj 3230 Travel

题目: 戳 题意: 有n个城市,一个人要在m天内travel 这n个城市.他每天在一个城市 要得到一个income 但是从一个城市到另一个城市也要有cost 如果呆在一个城市 也就是在矩阵中的从i到i表示呆在这个城市的cost nm 都小于100 然后给出n*n的矩阵 cost cost[i][j]表示的是从i到j的花费 然后一个m*n的矩阵 income[i][j]表示第i天在城市j得到的钱. 最后要m天后total income 最大 求这个最大值. 分析: dp d!p! 我觉得我写了个很

hdu 2571 命运(水DP)

题意: M*N的grid,每个格上有一个整数. 小明从左上角(1,1)打算走到右下角(M,N). 每次可以向下走一格,或向右走一格,或向右走到当前所在列的倍数的列的位置上.即:若当前位置是(i,j),可以走到(i,k*j) 问取走的最大和是多少. 思路: 水DP...边界的初始化要考虑.(因为有负数). 代码: int n,m; int a[30][1005]; int dp[30][1005]; int main(){ int T; cin>>T; while(T--){ cin>&g

hdu5074 Hatsune Miku 2014鞍山现场赛E题 水dp

http://acm.hdu.edu.cn/showproblem.php?pid=5074 Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 243 Problem Description Hatsune Miku is a popular vi

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

[dp] poj 1015 Jury Compromise

题目链接: http://poj.org/problem?id=1015 Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24438   Accepted: 6352   Special Judge Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury

HDU 2084 数塔 (水DP)

题意:.... 析:从下往上算即可,水DP. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <

CodeForces 706C Hard problem (水DP)

题意:对于给定的n个字符串,可以花费a[i]  将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][maxn],d[0][i]表示第 i 个不反转是最小花费,d[1][i]表示第 i 个反转最小花费,那么剩下的就很简单了么, 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio>