欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UPC 5157: Treasure Map

程序员文章站 2024-03-17 22:00:28
...

5157: Treasure Map

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


题意:给n个矿,每个矿每天减少d,给m条路,从u点到v点花费k天,每天不能呆在同一个地方。求可以得到的矿产最大值。


思路:

预处理一下每个矿到第j天的剩余,然后对每个点的边DP,记录max值就是ans。

#include <cstdio>
#include <algorithm>
#include <functional>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=1e3+10,mod=1e9+7,inf=(1ll<<60);
int n,m;
int g[maxn][maxn],d[maxn];        //g[i][j] 表示第i个矿到第j天的剩余   ,d[i]表示第i个矿每天减少量;
ll dp[maxn][maxn],ans;
vector<pair<int,int> >v[maxn];   //pair 头文件是<vector>;

int main(){
    memset(g,0,sizeof(g));
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++){
        scanf("%d%d",&g[i][0],&d[i]);
        dp[i][0]=inf;
    }
    ans=dp[1][0]=g[1][0];
    int u,vv,c;
    for (int i=1;i<=m;i++){
        scanf("%d%d%d",&u,&vv,&c);
        v[u].push_back(make_pair(vv,c));
        v[vv].push_back(make_pair(u,c));
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=g[i][0];j++){
            g[i][j]=(g[i][0]-d[i]*j<0)?0:g[i][0]-d[i]*j;
        }
    }
    int sz;
    for (int j=1;j<maxn;j++){
        for (int i=1;i<=n;i++){
            sz=v[i].size();   //对这个点的边进行遍历;
            for (int k=0;k<sz;k++){
                if(j-v[i][k].second>=0){
                    if(dp[v[i][k].first][j-v[i][k].second]>0){
                        dp[i][j]=max(dp[i][j],dp[v[i][k].first][j-v[i][k].second]+g[i][j]);
                    }
                }
            }
            ans=max(ans,dp[i][j]);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

相关标签: DP 入门stl