Treasure Map(DP+链式前向星)

问题 J: Treasure Map

时间限制: 1 Sec   内存限制: 128 MB
提交: 112   解决: 22
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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 2

1 2 1

样例输出

42

提示

题意:有n个金矿,m条路,一开始在1号金矿,每个金矿每天会丢失一定的价值,问最终能得到的最大的价值是多少?

题解:对于每个金矿直接扩展出去,暴力DP(果然DP的精髓就是暴力吗o(╥﹏╥)o),用链式前向星记录与每个金矿有路相连的金矿,枚举天数的时候遍历每一个金矿,对于每一个金矿走到所有能到达的金矿,但是注意能遍历的金矿必须是已经到达过的,这样状态转移方程就是dp[i+edge[k].w][edge[k].to]=max(dp[i][j]+max(0, a[edge[k].to]-(i+edge[k].w-1)*b[edge[k].to]), dp[i+edge[k].w][edge[k].to]);枚举第j个金矿,与j有关系的是edge[k].to金矿,由j号走到edge[k].to,由j号金矿的状态转移过去。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
int n, m, k=0, head[1005], dp[1005][1005], a[1005], b[1005];
struct node{
    int to, next, w;
}edge[1000005];
void add(int u, int v, int w)
{
    edge[k].to=v;
    edge[k].w=w;
    edge[k].next=head[u];
    head[u]=k++;
}

int main()
{
    int xx=0;
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d", &a[i], &b[i]);
        if(a[i]/b[i]+1>xx)  xx=a[i]/b[i]+1;
    }
    for(int i=0;i<m;i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);add(b, a, c);
    }
    memset(dp, 0, sizeof(dp));
    dp[1][1]=a[1];
    int ans=0;
    for(int i=1;i<=xx;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(!dp[i][j])  continue;
            for(int k=head[j];k!=-1;k=edge[k].next)
            {
                dp[i+edge[k].w][edge[k].to]=max(dp[i][j]+max(0, a[edge[k].to]-(i+edge[k].w-1)*b[edge[k].to]), dp[i+edge[k].w][edge[k].to]);
                ans=max(ans, dp[i+edge[k].w][edge[k].to]);
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值