UPC 5157 Treasure Map

5157: Treasure Map

时间限制: 1 Sec   内存限制: 128 MB
提交: 103   解决: 19
[ 提交][ 状态][ 讨论版][命题人: 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

提示

来源

Southeast USA ICPC 2017 


用dp[i][j]表示第j天在i号金矿最多一共能拿到多少金子。对于每个dp[i][j]!=0,它所能到达的金矿dp[to][j+cost]=max(dp[to][j+cost],dp[i][j]+gold(to,j+cost))。

#include <bits/stdc++.h>
using namespace std;
int cnt=0;
struct ed
{
  int to,next,cost;
}edge[2005];
int head[2005];
void add_edge(int from,int to,int cost)
{
    edge[cnt].to=to;
    edge[cnt].cost=cost;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}
int g[1005],d[1005];
int dp[1005][5005];
int gold(int num,int day)
{
    return max(0,g[num]-d[num]*(day-1));
}
 
int main()
{
    memset(head,-1,sizeof(head));
    int n,m;
    cin>>n>>m;
    int max_day=1;
    for (int i=1;i<=n;i++)
    {
        cin>>g[i]>>d[i];
        if (g[i]/d[i]+2 > max_day) max_day=g[i]/d[i]+2;
    }
    for (int i=0;i<m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        add_edge(a,b,c);
        add_edge(b,a,c);
    }
 
    dp[1][1]=gold(1,1);
 
    for (int i=1;i<=max_day;i++)
    {
        for (int j=1;j<=n;j++)
        {
            if (dp[j][i]!=0)
            {
                for (int k=head[j];k!=-1;k=edge[k].next)
                {
                    //cout<<i<<" "<<j<<" "<<edge[k].to<<endl;
                    if (i+edge[k].cost>max_day) continue;
                    dp[edge[k].to][i+edge[k].cost]=max(dp[edge[k].to][i+edge[k].cost],dp[j][i]+gold(edge[k].to,i+edge[k].cost));
                }
            }
        }
    }
    int ans=0;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=max_day;j++)
            if (dp[i][j]>ans) ans=dp[i][j];
    cout<<ans<<endl;
 
}

猜你喜欢

转载自blog.csdn.net/guess_guess_who_i_am/article/details/80141424
UPC
今日推荐