Treasure Map(背包变形)

题目描述

You have found a treasure map! The map leads you to several gold mines. The mines  each produce gold each day, but the amount of gold that they produce diminishes each day. There are paths between the mines. It may take several days to go from one mine to another. You can collect all of the day’s gold from a mine when you are there, but you have to move on, you cannot stay for multiple days at the same mine. However, you can return to a mine after leaving it. 

输入

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will begin with a line containing two integers n (2 ≤ n ≤ 1,000) and m (1 ≤ m ≤ 1,000), where n is the number of mines, and m is the number of paths.  
The next n lines will each describe a mine with two integers, g (1 ≤ g ≤ 1,000) and d (1 ≤ d ≤ 1,000), where g is the amount of gold mined on day 1, and d is the amount by which the gold haul diminishes each day. For example, if g=9 and d=4, then on day 1, the mine produces  9,  on  day  2  it  produces  5,  on  day  3  it  produces  1,  and  from  day  4  on,  it produces  0  (the  mines  cannot  produce  negative  amounts  of  gold).  The  mines  are numbered 1..n in the order that they appear in the input, and you start at mine 1 on day 1. 
The next m lines will each describe a path with three integers, a, b (1 ≤ a < b ≤ n) and t (1 ≤ t ≤ 100), where the path goes from mine a to mine b, and takes t days to traverse. The paths go in both directions, so that a path that goes from a to b can also be used to go from b to a. 

输出

Output a single integer, which is the maximum amount of gold that you can collect. 

样例输入

2 1

10 1

10 1
1 2 1

样例输出
42

题意:有n个金矿,每一天他都会生产金子,但是每天生产的金子会减少d,初始值生产是g,你只能拿当天生产的金子,金矿与金矿之间有路,需要花t天才能走过去。现在问你最多最后能拿多少金子。
注意 不能在同一个金矿停留。

思路:第一维代表是第几天,第二维代表第几个金矿,dp的值肯定是最后的金子数。

dp[i][j]代表第i天到了第j个金矿的金子数。

因为这金矿的状态一定是上一个金矿走过去的,花了t天,v是上一个金矿的标号,最后那一部分是金矿的生产值

dp[i][j]=max(dp[i][j],dp[i-t][v]+max(0,g[j]-d[j]*(i-1)))
金矿的产出值不能为负数 所以要和 0 比较

有了DP方程,建图,然后就更新ans。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
struct node
{
    int from;
    int time;
};

vector <node> edge[1005];
ll dp[1005][1005];
int g[1005];
int d[1005];
int main()
{
    int n,m;
    int a,b,t;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ms(dp,0);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&g[i],&d[i]);
        for(int i=1;i<=m;i++)
        {

            scanf("%d%d%d",&a,&b,&t);
            node q;
            q.time=t;
            q.from=b;
            edge[a].push_back(q);//正反两个路
            q.from=a;
            edge[b].push_back(q);
        }
        dp[1][1]=g[1];
        for(int i=1;i<=1001;i++)//总时间 (暴力天数)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=0;k<edge[j].size();k++)
                {
                    int from=edge[j][k].from;
                    int time=edge[j][k].time;
                    if(i-time>=1 && dp[i-time][from])
                        dp[i][j]=max(dp[i][j],dp[i-time][from]+max(0,g[j]-(i-1)*d[j]));
                }

            }
        }
        ll ans=0;
        for(int i=0;i<=1001;i++)
            for(int j=1;j<=n;j++)
                ans=max(ans,dp[i][j]);
                    printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值