中石油5157: Treasure Map(无向图dp)

5157: Treasure Map

时间限制: 1 Sec   内存限制: 128 MB
提交: 111   解决: 21
[ 提交][ 状态][ 讨论版][命题人: 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边,每个点u有一个产金量,每个边走过耗费day天,如果是第k天到达u点,可获得g[u]-k*d[u]的金子。

若从1点出发,问最多可收集多少金子。终点不定

【分析】

不会写、、、、、、

dp[i][j]表示到达i点时,已走过j天,那么当前点的状态来自他的邻接点x

dp[i][j] = dp[x][j-day] + max(0,g[i]-d[i]*j)     //day是邻接边权值

枚举j,dp每个点

【代码】

#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
typedef long long ll;
const int N=1e6+5;
const int INF=0x3f3f3f3f;
struct node{
    int s,t,day,next;
}e[N];
int head[2010],cnt;
void add(int u,int v,int cap)
{
    e[cnt]=node{u,v,cap,head[u]};
    head[u]=cnt++;
}
int n,m;
int g[N],d[N];
int dp[1010][1010];
void init(){
    memset(head,-1,sizeof(head[0])*(n+1));
    cnt=0;
}
int main()
{
    int u,v,day;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        init();
        for(int i=1;i<=n;i++){
            scanf("%d%d",&g[i],&d[i]);
        }
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&day);
            add(u,v,day);
            add(v,u,day);
        }
        dp[1][0]=g[1];
        int ans=0;
        rep(j,0,1001)rep(u,1,n)
        {
            for(int i=head[u];~i;i=e[i].next)
            {
                if(j>=e[i].day&&dp[e[i].t][j-e[i].day])
                    dp[u][j]=max(dp[u][j],dp[e[i].t][j-e[i].day]+max(0,g[u]-d[u]*j));
            }
            ans=max(ans,dp[u][j]);
        }
        printf("%d\n",ans);
    }
}

/**************************************************************
    Problem: 5157
    User: ldu_sc03
    Language: C++
    Result: 正确
    Time:96 ms
    Memory:29128 kb
****************************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值