问题 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. 【LGR-052】洛谷9月月赛II(加赛)

    题解: 没打... ab题满世界都过了应该没什么意思 c题是个比较有意思的思维题(先看了题解才会的...) 我们考虑这么一件事情 没钥匙的人出门后 门一定是开着的 他进来的时候,门一定是开着的 其他时 ...

  2. LCA算法解析-Tarjan&倍增&RMQ

    原文链接http://www.cnblogs.com/zhouzhendong/p/7256007.html UPD(2018-5-13) : 细节修改以及使用了Latex代码,公式更加美观.改的过程 ...

  3. 阿里巴巴Java开发规范手册

      Java开发手册 版本号 制定团队 更新日期 备  注 1.0.0 阿里巴巴集团技术部 2016.12.7 首次向Java业界公开 一.编程规约 (一) 命名规约 1.   [强制]所有编程相关命 ...

  4. day63 django-模板语言

    我们的功能是需要解耦的,从开始就一直在强调这一点,所以我们的函数需要另外放到一个单独的文件里面,一般都是放到views文件里面,views叫做视图,一般术语叫做视图函数,用来进行各种逻辑判断的,需要一 ...

  5. 6、Qt Project之音视频播放

    音视频播放  这里简单的制作了一个音乐播放器,播放器的界面设计如下所示: Step1:这里是界面对应的HTML文件: <?xml version="1.0" encoding ...

  6. js(javaScript)的各种事件触发,以常见为主eg:onclick

    js的各种事件触发,以常见为主eg:onclick1.onclick,点击后触发事件 (1)<h1 onclick="this.innerHTML='谢谢!'">请点击 ...

  7. 在mybatis中调用存储过程的时候,不能加工语句

    select count(0) from ({call pkg_business.P_ZZS_LYFPHJSKJQK ('2018-04')}) 这是错误的.

  8. HDFS常用API(1)

    一.HDFS集群API所需要jar包的maven配置信息 <dependency> <groupId>org.apache.hadoop</groupId> < ...

  9. 【JavaScript】对象

    No1: typeof操作符获取对象的类型 null的类型是object,Array的类型也是object,如果我们用typeof将无法区分出null.Array和通常意义上的object——{}. ...

  10. ES2018正则表达式更新

    如果你是一个初学者,这篇文章可以拓展你对正则表达式用法的理解,不过建议你先阅读一些正则表达式入门文章,比如经典的<正则表达式30分钟入门教程>.如果你对正则表达式有一定的认识,那么这篇文章 ...