排位赛3-Time is Mooney

排位赛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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值