2019牛客暑期多校训练营(第六场)J Upgrading Technology

链接:https://ac.nowcoder.com/acm/contest/886/J
来源:牛客网
 

Rowlet is playing a very popular game in the pokemon world. Recently, he has encountered a problem and wants to ask for your help.

In this game, there is a technology tree system. There are n kinds of technology in this game, each of them has m levels numbered from 1 to m. In the beginning, all technologies have no level (regard as level 0). When the i-th technology is at the (j - 1)-th level, the player can pay cijc_{i j}cij​ pokedollars (currency used in this game) to upgrade this technology into the j-th level. However, sometimes upgrading is so easy that the cost might be negative, which implies the player may gain profit from upgrading technologies.

Moreover, if all technologies have been upgraded to level j, the player will gain an additional profit of djd_{j}dj​ pokedollars. However, sometimes too many technologies of the same level might be confusing, hence the profit can be negative as well.

Rowlet wants to determine the optimal strategy that can bring him the most pokedollars. Help him to find the maximum gain. Note that Rowlet may upgrade nothing, and in that case, the profit is zero.

输入描述:

There are multiple test cases. The first line contains an integer T (1≤T≤101 \leq T \leq 101≤T≤10), indicating the number of test cases. Test cases are given in the following.

For each test case, the first line contains two integers n, m (1≤n,m≤10001 \leq n, m \leq 10001≤n,m≤1000), representing the number of technologies and the number of levels respectively.

The i-th of the next n lines contains m integers, where the j-th number is cijc_{i j}cij​ (−109≤cij≤109-10^{9} \leq c_{i j} \leq 10^{9}−109≤cij​≤109).

The last line contains m integers, where the j-th number is djd_{j}dj​ (−109≤dj≤109-10^{9} \leq d_{j} \leq 10^{9}−109≤dj​≤109).

We ensure that the sum of n⋅mn \cdot mn⋅m in all test cases is at most 2×1062 \times 10^{6}2×106.

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1, and y denotes the answer(in pokedollars) to this test case.

示例1

输入

复制

2
2 2
1 2
2 -1
4 1
3 3
1 2 3
1 2 3
1 2 3
6 7 8

输出

复制

Case #1: 2
Case #2: 4

题意:你有n个科技可以升级,最多每个科技升到m级,每个科技升级可能赚钱,也可能亏钱,当n个科技都升同一个等级你能够得到一个奖金(也可能亏钱)

分析:这题使用的是dp,dp[i][j]表示前i个科技等级最低为j时亏损最少的钱(负数就是赚钱了),

转移方程:dp[i][j]=min(minn+a[i][j],dp[i-1][j]+b[i][j]);

minn是前i-1个科技最低等级不低于j是亏损最少

a[i][j]表示第i个科技j级是亏损的钱

b[i][j]比表示第i个科技等级不低于j级时亏损的钱

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[1005][1005],a[1005][1005],b[1005][1005],c[1005];
int main(){
	ll T,n,m;
	cin>>T;
	for(ll t=1;t<=T;t++){
		memset(a,0,sizeof a);
		cin>>n>>m;
		for(ll i=1;i<=n;i++){
			for(ll j=1;j<=m;j++){
				cin>>a[i][j];
				a[i][j]+=a[i][j-1];
			}
			ll minn=1e18;
			for(ll j=m;j>=0;j--){
				minn=min(a[i][j],minn);
				b[i][j]=minn;
			}
		}
		for(ll i=1;i<=m;i++){
			cin>>c[i];
			c[i]+=c[i-1];
		}	
		for(ll j=1;j<=m;j++) dp[1][j]=a[1][j];
		for(ll i=2;i<=n;i++){
			ll minn=1e18;
			for(ll j=m;j>=0;j--){
				minn=min(dp[i-1][j],minn);
				dp[i][j]=min(minn+a[i][j],dp[i-1][j]+b[i][j]);
			}
		}
		ll ans=0;
		for(int i=0;i<=m;i++)
			ans=max(ans,c[i]-dp[n][i]);
		printf("Case #%d: %lld\n",t,ans);		
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值