Time is Mooney(dp)

题目描述
Bessie is conducting a business trip in Bovinia, where there are N (2≤N≤1000) cities labeled 1…N connected by M (1≤M≤2000) one-way roads. Every time Bessie visits city i, Bessie earns mi moonies (0≤mi≤1000). Starting at city 1 Bessie wants to visit cities to make as much mooney as she can, ending back at city 1. To avoid confusion, m1=0.
Mooving between two cities via a road takes one day. Preparing for the trip is expensive; it costs C⋅T2moonies to travel for T days (1≤C≤1000).

What is the maximum amount of moonies Bessie can make in one trip? Note that it may be optimal for Bessie to visit no cities aside from city 1, in which case the answer would be zero.

输入
The first line contains three integers N, M, and C.
The second line contains the N integers m1,m2,…mN.

The next M lines each contain two space-separated integers a and b (a≠b) denoting a one-way road from city a to city b.

输出
A single line with the answer.

样例输入
3 3 1
0 10 20
1 2
2 3
3 1

样例输出
24

提示
The optimal trip is 1→2→3→1→2→3→1. Bessie makes 10+20+10+20−1⋅62=24 moonies in total.

思路
有题可得T<1000,否则条件无法成立,因此之间DP推即可

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1005;
const int M=2005;
const int INF=0x3f3f3f;
const ull sed=31;
const ll mod=1e9+7;
const double eps=1e-9;
typedef pair<int,int>P;
typedef pair<double,double>Pd;
 
int n,m,c;
ll val[N],ans,dp[2][N];
 
struct node
{
    int to,index;
    ll w;
}E[M];
 
int head[N],cnt;
 
void add(int u,int v)
{
    E[++cnt].to=v;
    E[cnt].w=val[v];
    E[cnt].index=head[u];
    head[u]=cnt;
}
 
int main()
{
    scanf("%d%d%lld",&n,&m,&c);
    for(int i=1;i<=n;i++) scanf("%lld",&val[i]);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    memset(dp,-1,sizeof(dp));
    dp[0][1]=0;
    for(int i=1;i<N;i++)
    {
        memset(dp[i%2],-1,sizeof(dp[i%2]));
        for(int u=1;u<=n;u++)
        {
            for(int j=head[u];j;j=E[j].index)
            {
                int v=E[j].to;
                ll cost=E[j].w;
                if(dp[(i+1)%2][u]>=0) dp[i%2][v]=max(dp[i%2][v],dp[(i+1)%2][u]+cost);
            }
        }
        ans=max(ans,dp[i%2][1]-c*i*i);
    }
    printf("%lld\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值