问题 I: T-shirt

时间限制: 1 Sec  内存限制: 64 MB

题目描述

JSZKC is going to spend his vacation! 
His vacation has N 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 M different T-shirt with different color. If he wears A color T-shirt this day and B color T-shirt the next day, then he will get the pleasure of 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. 

输入

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

  • The first line of the input contains two integers N,M (2 ≤ N≤ 100000, 1 ≤ M≤ 100), giving the length of vacation and the T-shirts that JSZKC has.
  • The next follows M lines with each line M integers. The jth integer in the ith line means f[i][j](1<=f[i][j]<=1000000).

There are no more than 10 test cases.

输出

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

meaning

给一个m×m的矩阵,data[i][j]表示第一天在i点第二天在j点的收益,问n天的最大收益。

solution

f[k][i][j] 表示第一天在i点,2^k天后在j点的最大收益。

f[0][i][j] = data[i][j];

f[k][i][j] = max(f[k][i][j],f[k-1][i][p]+f[k-1][p][j]);

d[i][j] 表示从i点到j点的最大收益。

code

#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxm = 105; ll f[20][maxm][maxm];
ll d[2][maxm][maxm];
int main() {
// IN_LB();
int n,m;
while(scanf("%d%d",&n,&m)!=EOF) {
memset(f,0,sizeof f);
memset(d,0,sizeof d);
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
scanf("%lld",&f[0][i][j]);
}
}
for(int k=1; k<20; k++)
for(int l=0; l<m; l++)
for(int i=0; i<m; i++)
for(int j=0; j<m; j++)
f[k][i][j] = max(f[k][i][j],f[k-1][i][l]+f[k-1][l][j]);
n--;
int cnt = 0;
for(int k=19; k>=0; k--) {
if(n>(1<<k)) {
n-=(1<<k);
for(int l = 0; l<m; l++) {
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
d[cnt][i][j]=max(d[cnt][i][j],d[cnt^1][i][l]+f[k][l][j]);
}
}
}
cnt^=1;
}
}
if(n==1) {
for(int l = 0; l<m; l++) {
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
d[cnt][i][j]=max(d[cnt][i][j],d[cnt^1][i][l]+f[0][l][j]);
}
}
}
}
ll ans = 0;
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) {
ans = max(ans,d[cnt][i][j]);
}
}
printf("%lld\n",ans);
}
return 0;
}

【倍增】T-shirt @2018acm徐州邀请赛 I的更多相关文章

  1. 【容斥+组合数】Massage @2018acm徐州邀请赛 E

    问题 E: Massage 时间限制: 1 Sec  内存限制: 64 MB 题目描述 JSZKC  feels  so  bored  in  the  classroom  that  he  w ...

  2. 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G

    问题 G: JS Window 时间限制: 2 Sec  内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...

  3. 一类SG函数递推性质的深入分析——2018ACM陕西邀请赛H题

    题目描述 定义一种有根二叉树\(T(n)\)如下: (1)\(T(1)\)是一条长度为\(p\)的链: (2)\(T(2)\)是一条长度为\(q\)的链: (3)\(T(i)\)是一棵二叉树,它的左子 ...

  4. 树链剖分的一种妙用与一类树链修改单点查询问题的时间复杂度优化——2018ACM陕西邀请赛J题

    题目描述 有一棵树,每个结点有一个灯(初始均是关着的).每个灯能对该位置和相邻结点贡献1的亮度.现有两种操作: (1)将一条链上的灯状态翻转,开变关.关变开: (2)查询一个结点的亮度. 数据规模:\ ...

  5. 2018 ICPC 徐州邀请赛 总结

    Day 0 上午在高铁里面,很困但是睡不着…… 中午到矿大报道. 食堂饭菜不错,有西瓜,就是有点辣. 下午热身赛,D题队友想了个假算法……GG. 评测机摸底考试正常进行. 热身赛之后精疲力尽,赶到宾馆 ...

  6. 徐州邀请赛 江苏 icpc I. T-shirt 矩阵快速幂

    题目 题目描述 JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-s ...

  7. 江苏 徐州邀请赛 icpc B Array dp 滚动数组模板

    题目 题目描述 JSZKC is the captain of the lala team. There are N girls in the lala team. And their height ...

  8. 台州学院maximum cow训练记录

    前队名太过晦气,故启用最大牛 我们的组队大概就是18年初,组队阵容是17级生詹志龙.陶源和16级的黄睿博. 三人大学前均无接触过此类竞赛,队伍十分年轻.我可能是我们队最菜的,我只是知道的内容最多,靠我 ...

  9. acm一路走来的体验和想法

    2017年参加天梯赛 作为大一萌新去参加人生第一场正规的比赛,学校拿钱砸的.我呢,是十分激动的,是时候检验下自己的编程水平了,也有一丝慌张,怕给团队抹黑. 然后呢,晒一下自己成绩. 我本来觉得我和他们 ...

随机推荐

  1. Visual stuio2015 升级 Update 3+安装.Net Core 安装包之后,无法创建Mvc项目

    原因: 怀疑是更新后缺少Web Frameworks and Tools 工具, 安装update3的时候提示异常 解决方法: 1.去微软 下载 Web Frameworks and Tools安装后 ...

  2. 期货大赛项目|二,DAL详解

    接口层就不重点讲述了,直接DAL层 DAL层 using System; using System.Collections.Generic; using System.Linq; using Syst ...

  3. P4715 「英语」Z 语言

    题解: 平衡树维护hash值 为了支持加入删除操作 x*base^y 其中y为他是第k大 同一般的维护方法,我们不用对每个节点维护他的hash值 而是只用记录他的x值(他的位置) 然后通过updata ...

  4. 【译】理解JavaScript中的柯里化

    译文开始 函数式编程是一种编程风格,这种编程风格就是试图将传递函数作为参数(即将作为回调函数)和返回一个函数,但没有函数副作用(函数副作用即会改变程序的状态). 有很多语言采用这种编程风格,其中包括J ...

  5. Python_列表常用操作

    %d   数字 %f    浮点 %s    字符串 字符串常用功能: .strip()   默认去掉字符串两边空格#或者在括号里注明去除什么 查看列表方法:dir(列表名) .append(元素): ...

  6. solr配置IKAnalyzer抛出ClassNotFoundException

    这个问题搞了很久,在QQ群上问了很久,关键很气人的是我居然被群主给开了.我也是醉了.我不知道我哪里得罪了那个solr群的群主. 废话不多说.抛出的异常如下: 刚开始一直认为是没有找到类,也就相当于没找 ...

  7. .net core cookie登录和session的 DataProtectionProvider 加入 redis

    string redisConnectionString = Configuration.GetSection("Storage:Redis").GetValue<strin ...

  8. P1052 过河 线性dp

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  9. JavaSE | 接口| 枚举| 注释| 异常

    包: 1.包的作用:(1)避免类的同名(区分类):类的全名称:包.类名 回忆:java.util.Scannerjava.util.Arraysjava.lang.Stringj(2)可以限定某些类或 ...

  10. fastadmin系统配置

    常规管理--->系统配置--->字典配置-->配置分组-->追加--填上键值-->回车 然后在点上图的+添加自定义的配置项(如果需要删除配置项,需要删除数据库中fa_co ...