2018徐州邀请赛I题 I. T-shirt

JSZKC is going to spend his vacation!

His vacation has NN days. Each day, he can choose a T-shirt to wear. Obviously, he doesn't want to wear a singer color T-shirt since others will consider he has worn one T-shirt all the time.

To avoid this problem, he has MM different T-shirt with different color. If he wears AA color T- shirt this day and BBcolor T-shirt the next day, then he will get the pleasure of f[A][B]f[A][B].(notice: He is able to wear one T-shirt in two continuous days but may get a low pleasure)

Please calculate the max pleasure he can get.

Input Format

The input file contains several test cases, each of them as described below.

  • The first line of the input contains two integers N,MN,M (2 \le N \le 100000, 1 \le M \le 100)(2≤N≤100000,1≤M≤100), giving the length of vacation and the T-shirts that JSZKC has.

  • The next follows MM lines with each line MM integers. The j^{th}jth integer in the i^{th}ith line means f[i][j]f[i][j] (1\le f[i][j]\le 1000000)(1≤f[i][j]≤1000000).

There are no more than 1010 test cases.

Output Format

One line per case, an integer indicates the answer.

样例输入

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

样例输出

2
9

题目来源

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest

思路: 本题就是让你求一个n-1的序列。f[a][b]+f[b][c]+f[c][d]+~~~~+  f[x][y]+f[y][z]的最大值。

那么第0 天(也就是题目中的第一天) 可以穿任意的衣服,那么第一天就根据第0 天的衣服确定最大价值,我们可以设dp[time][i][j] 表示第0天穿i 第time天穿j 的最大价值,那么我们就可以得出 dp[a+b][i][j] =max(dp[a][i][k]+dp[b][k][j])。那么对于该式子我们可以用快速幂处理来降低时间复杂度。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N =105;

struct node
{
    ll ma[N][N];
};
int n,m;

node mut(node a,node b)
{
    node ans;
    memset(ans.ma,0,sizeof(ans.ma));
    for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
            for(int k=1;k<=m;k++){
                ans.ma[i][j]=max(ans.ma[i][j],a.ma[i][k]+b.ma[k][j]);
            }
        }
    }
    return ans;
}

node quick_pow(node a,int k)
{
    node ans;
    memset(ans.ma,0,sizeof(ans.ma));
    while(k)
    {
        if(k&1) ans=mut(ans,a);
        a=mut(a,a);
        k>>=1;
    }
    return ans;
}

int main()
{
    node tmp;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                scanf("%lld",&tmp.ma[i][j]);
            }
        }

        node ans=quick_pow(tmp,n-1);

        ll maxx=0;
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                maxx=max(maxx,ans.ma[i][j]);
            }
        }
        printf("%lld\n",maxx);
    }

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值