SPOJ CATTACK - Counter attack (阅读理解题 DP)

https://www.spoj.com/problems/HACKING/en/

CATTACK - Counter attack

no tags 

 

At our soccer training camp, we have rehearsed a lot of motion sequences. In case we are defending, all players except the two strikers of our team are in our half. As soon as we are getting the ball, we are starting a counterattack with a long-range pass to one of our strikers. They know each others motion sequences and may pass the ball to the other striker at fixed points.

There are a lot of decisions: the defender has to select the striker to pass the ball to, and the ball possessing striker has to decide at each of the n fixed points if to pass to the other striker or to run and to dribble. At the last position in the motion sequence of a striker he shoots on the goal. Each of the four actions (long-range pass, dribble, pass, and shoot on the goal) may fail (e.g. because of a defending player of the opposite team) - so our coach has assigned difficulties.

What is the minimal difficulty of a goal assuming your team plays optimally?

example image

In the example depicted in the picture, the defending player (cross in left half) passes the ball to one of the strikers (crosses in right half). The strikers move along fixed paths simultaneously. At each of the fixed positions (circles), the ball possessing striker either dribbles with the ball or passes to the other striker. At the last position, he shoots on the goal.

Input

The first line of the input consists of the number of test cases c that follow (1 ≤ c ≤ 100). Each test case consists of five lines. The first line of each test case contains n (2 ≤ n ≤ 100000), the number of fixed points in each strikers motion sequence. It is followed by l0l1s0 and s1, the difficulty of a long-range pass to the corresponding striker and the difficulties of the shoots of the strikers. Each striker is described in two lines (first striker 0, then striker 1): The first line contains n-1 difficulties, where the ith number stands for passing from point i to the other player at point i + 1. The second line also contains n-1 difficulties, where the ith number stands for dribbling from point i to point i+1. You may safely assume that each difficulty is a non-negative integer less than 1000.

Output

For each test case in the input, print one line containing the minimal difficulty of a move sequence leading to a goal.

Example

Input:
2
3 3 5 7 999
9 13
60 5
22 6
5 5
5 3 5 7 999
9 13 8 4
60 5 17 13
22 6 15 11
5 5 18 29

Output:
23
42

题意:就是一个起点,一个终点,然后分成上下两路传球。 有两个球员,上路的球员可以自己运球在上路行动,也可以将球穿给下路球员。

下路球员也可以自己运球前进或者传球给上路球员。 每次运球从一个点到另一个点,或者传球从一个点从另一个点有一个权值,问从起点出发,将球射入球门的最小权值。

题解:画个图很容易发现就是个交叉的传球运球路线。每个点的权值,要么是从同一路线的前一个点转移过来,要么是从另一个路线传球过来的。因此很容易想到dp

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

dp前一维表示路线,后一维表示哪个点,a[] , b[] 数组记录同个路线的权值,传球的权值 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<vector> 
using namespace std;
#define inf 0x3f3f3f
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;
typedef long long ll;
const int maxn = 1e5+5;
int n,l0,l1,s0,s1;

int dp[2][maxn];
int a[2][maxn],b[2][maxn];
int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n>>l0>>l1>>s0>>s1;
		mem(dp,0);
		dp[0][1] = l0;
		dp[1][1] = l1;
		//dp[0][n] = s0;
	//	dp[1][n] = s1;
		for(int i = 0; i <= 1; i++){
			for(int j = 1; j <= n-1; j++){
				scanf("%d",&a[i][j]);
			}
		}
		for(int i = 0; i <= 1; i++){
			for(int j = 1; j <= n-1; j++){
				scanf("%d",&b[i][j]);
			}
		}
		for(int i = 2; i <= n; i++){
			dp[0][i] = min(dp[0][i-1]+a[1][i-1],dp[1][i-1]+b[0][i-1]);
			dp[1][i] = min(dp[1][i-1]+b[1][i-1],dp[0][i-1]+a[0][i-1]);
		}
		ll ans = min(dp[0][n]+s0,dp[1][n]+s1);
		
		cout<<ans<<endl;
	}
} 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值