5157: Treasure Map

5157: Treasure Map

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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

 

 

说白了就是个动态规划,结果一点小细节搞得我= =

现在还不明白为什么要多更新一次时间(早知道无脑暴力多好,时间直接2--1000,完事)

 

#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 <vector>
#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;
int min3(int a,int b,int c)
{
    return min(min(a,b),c);
}
int max3(int a,int b,int c)
{
    return max(max(a,b),c);
}
int gcd(int x, int y)
{
    if(y==0)
        return x;
    return gcd(y, x%y);
}
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}

ll dp[1005][1005];
int cow[1005][1005];
vector<int> Edg[1005];
int vv[1005][2];
int main()
{
    int n,m;

    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        memset(cow,0,sizeof(cow));

        for(int i=0;i<1005;i++)
            Edg[i].clear();

        int mt=0;

        for(int i=1;i<=n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vv[i][0]=x;
            vv[i][1]=y;
            mt=max(mt,(x+1)/y);
        }
        int xx=0;
        for(int i=0;i<m;i++)
        {
            int x,y,z;

            scanf("%d%d%d",&x,&y,&z);
            if(!cow[x][y]  ||  cow[x][y]>z)
                cow[x][y]=cow[y][x]=z;
            Edg[x].push_back(y);
            Edg[y].push_back(x);
            xx=max(xx,z);
        }
        dp[1][1]=vv[1][0];ll ans=0;

        for(int t=2;t<=mt+xx;t++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<Edg[i].size();j++)
                {
                    int tt=cow[i][Edg[i][j]];

                    if(t-tt<1)
                        continue;

                    if(dp[t-tt][Edg[i][j]])
                    {
                        ll temp=vv[i][0] - (t-1)*vv[i][1];

                        temp=temp>=0?temp:0;

                        dp[t][i]=max(dp[t][i],dp[t-tt][Edg[i][j]]+temp);

                        ans=max(ans,dp[t][i]);

                        //printf("%d %d ---%lld\n\n",i,Edg[i][j],ans);
                    }
                }
            }
        }

        printf("%lld\n",ans);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值