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!

我觉得我写了个很对的dp,一直就在wa!!!!!!

然后按照哀家的思想刷。 就对了! 就 对 了!

不说了,贴代码!!!

#include<cstdio>
#include<algorithm>
#include<cstring>
#define inf  -0x7fffffff
using namespace std;
int n,m,ans;
int dp[102][102];
int income[102][102],ex[102][102];
int main()
{
    while(scanf("%d%d",&n,&m))
    {
        if(n == 0 && m == 0)
        break;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&ex[i][j]);
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&income[i][j]);
            }
        }
        memset(dp,-inf,sizeof(dp));
        dp[0][1]=0;
        for(int i=1;i<=n;i++) dp[1][i]=income[1][i]-ex[1][i];  //第一天去第i个城市
        for(int i=2;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=1;k<=n;k++)
                {
                    dp[i][j]=max(dp[i][j],dp[i-1][k]+income[i][j]-ex[k][j]);
                }
            }
        }
        ans=inf;
            for(int j=1;j<=n;j++)
            {
                ans=max(ans,dp[m][j]);
            }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-02 01:29:41

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

[水+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&q

POJ 3230 【DP】

题意: 某货旅行,在n个城市呆m天. 给出从第i个城市到第j个城市的路费,或者留在某个城市的生活费. 给出在第i天在第j个城市的收益. 可以在城市之间任意穿梭逗留没有其他特殊要求. 求收益最大是多少. 思路: dp[i][j]代表这货在第i天在第j个城市的最大收益. 然后状态转移方程是dp[i][j]=max(dp[i-1][1..n]+枚举目标城市计算收益)[这里少写了目标城市的那层循环,一共三层循环] #include<stdio.h> #include<string.h> #

{POJ}{动态规划}

动态规划与贪心相关: {POJ}{2479}{Maximum Sum} (DP) 摘要: 题意:给定n个数,求两段连续子列的最大和.思路:先从左向右dp,求出一段连续子列的最大和,再从右向左dp,求出两段连续子列的最大和,方法还是挺经典的. {POJ}{1036}{Gansters} (DP) 摘要: 题意:有个伸缩门,门的宽度0~K,每个时间可以伸长或缩短1个单位,有N个流氓,他们在T时刻到达,如果这时门的宽度正好与他们的stoutness相等时,便可获得一定的收入,问最大的收入是多少. 思路

15年-ICPC长春-网络赛

ID name status one word    POJ 5437 Alisha’s Party 赛后AC. 优先队列,模拟.对时间t排序 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <queue> using namespace std; struct Node { char name[210]; int v

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ 3667 Hotel

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16782   Accepted: 7303 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie

[题解]poj Meteor Shower

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16313   Accepted: 4291 Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious fo

POJ 1847 Tram 【最短路,spfa算法,题意理解是关键呀!!】

Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13468   Accepted: 4954 Description Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to t