排位赛3-Time is Mooney
题目
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⋅T2 moonies 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.
题意
Bessie打算旅游,她每到一个城市都要打工赚钱,第i个城市打工可以赚到ai元,n个城市间由m条单向道路,到一个城市打工游玩要一天时间,旅行T天要花费C*T^2元,求她按什么旅行顺序能赚到最多的钱。
解法
因为ai<=1000,n<=1000,C<=1000,所以可以求得最多只能旅行1000天,那我们就可以用dp来做f[i][j]第i天旅行到j城市最多能赚多少钱,所以可得状态转移方程f[i+1][j]=max(f[i][k]+a[j]-c*(2*i+1),f[i+1][j])
代码:
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
struct CC
{
int next,to;
}edge[2010];
int a[2010],f[1010][2010],head[2010];
bool vis[1010][1010];
int cnt,n,m,c,ans,x,y;
void addedge(int x,int y )
{
edge[++cnt].next=head[x];
edge[cnt].to=y;
head[x]=cnt;
}
int main()
{
scanf("%d%d%d",&n,&m,&c);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
}
for (int i=0;i<=1001;i++) for (int j=1;j<=n;j++) f[i][j]=-1;
vis[0][1]=true;
f[0][1]=0;
for (int i=0;i<=1000;i++)
{
for (int j=1;j<=n;j++)
if (vis[i][j])
{
for (int k=head[j];k!=0;k=edge[k].next)
{
int v=edge[k].to;
f[i+1][v]=max(f[i][j]+a[v]-c*(2*i+1),f[i+1][v]);
if (f[i+1][v]>=0) vis[i+1][v]=true;
}
}
}
for (int i=0;i<=1001;i++) ans=max(ans,f[i][1]);
printf("%d",ans);
return 0;
}