CC COUPON(Online Shopping-裸DP)

Online Shopping


lice likes to shop online. The main reason is that she gets a lot of discounts! There are M online shops from where she purchases her items. The prices of the same item vary in both the shops. She needs to buy N items in all. And each she can buy each item in any one of the shops. All the shops offer certain discounts. If she buys the ith item in one shop, she gets a discount coupon for the i+1th item in the same shop. But if she purchases the i+1th item from a different shop, she cannot use the discount coupon.
For example, if she buys the ith item in Shop j and gets a discount of X rupees. Let the price of i+1th item be Y rupees in shop j and Z rupees in shop k. If she purchases the i+1th item from shop j she can use the discount coupon and hence, will have to pay Y-X rupees. If she purchases the i+1th item from shopk she cannot use the discount coupon and hence, will have to pay Z rupees.

Note: If the discount given is more than price of the item in the shop, i.e. if Y-X < 0, the item is given free but she does NOT get any money back.

Alice wants to spend as less as possible and wants your help. She starts buying from item 1 and goes on till item N in that order. Your task is simple. Tell her the least amount to spend to get all the N items.

Input:

First line of input contains a single integer T, the number of test cases.

Each test case starts with a line consisting of two space separated integers N & M.
The next N lines consist of M space separated integers each. The jth number on the ith line denotes the price of ith item in the jth shop.

The next N lines consist of M space separated integers each. The jth number on the ith line denotes the value of discount coupon given after purchasing ith item in the jth shop.





Output:

For each test case, output on a separate line the least amount that has to be spent to get all the N items.



Constraints:

1 ≤ T ≤ 10
2 ≤ N, M ≤ 100000
0 ≤ Prices, Discounts ≤ 1000000
1 ≤ T * N * M ≤ 1000000

Example:

Input:

2
2 2
3 4
1 2
1 0
0 1
2 4
1 2 3 4
4 3 2 1
2 3 2 1
1 2 1 1


Output:

3
2

Scoring:

You will be awarded 40 points for solving the problem correctly for M = 2.

Another 20 points for solving the problem correctly for M <= 100.

Remaining 40 points will be awarded for solving the problem correctly for M > 100.


裸nmDP

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<map>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (100000+10)
#define MP make_pair
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
map<pair<int,int>,ll> f,cost,dis;
int T,n,m;
int main()
{
//	freopen("COUPON.in","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        For(i,n) For(j,m) {ll p;scanf("%lld",&p);cost[MP(i,j)]=p; };
        For(i,n) For(j,m) {ll p;scanf("%lld",&p);dis[MP(i,j)]=p;if (i<n&&cost[MP(i+1,j)]-dis[MP(i,j)]<0) dis[MP(i,j)]=cost[MP(i+1,j)];}
        ll minlast=0,minnow=0;
        For(i,n)
        {
            minlast=minnow,minnow=INF;
            For(j,m)
            {
                if (i==1) f[MP(i,j)]=cost[MP(i,j)];
                else f[MP(i,j)]=min(minlast+cost[MP(i,j)],f[MP(i-1,j)]+cost[MP(i,j)]-dis[MP(i-1,j)]);
                if (j==1) minnow=f[MP(i,j)];
                else minnow=min(minnow,f[MP(i,j)]);
            }
        }
        cout<<minnow<<endl;
    }

	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值