问题 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. Tychonov Theorem

    (Remark: The proof presented in this post is a reorganization and interpretation of that given by Ja ...

  2. Azure 国内版 如何用powershell修改linux系统的密码

    国内版不像国际版本那样,一个UI按钮就解决问题,国内版很多功能上线比较慢,我们只能用powershell工具进行命令行 式的更改,也当温习一下命令了,好久不用了. $vm = Get-AzureVM ...

  3. 【Android】Bitmap加载图片错误 java.lang.OutOfMemoryError: bitmap size exceeds VM budget

    今天测试程序的时候出现下面的错误日志信息,程序当场挂掉 07-09 14:11:25.434: W/System.err(4890): java.lang.OutOfMemoryError: bitm ...

  4. seafile增加邮件服务功能

    这个很简单哈,直接上配置.此处我用的163邮箱 vim /opt/seafile/conf/seahub_settings.py ### 163邮箱配置测试 ### EMAIL_USE_SSL = F ...

  5. eclipse启动web应用

    在建好web项目的基础上: (1)配置tomcat服务器 点击window---->Preference----->Server---->Runtime Environment--- ...

  6. Python学习(二十八)—— Django模板系统

    转载自http://www.cnblogs.com/liwenzhou/p/7931828.html Django模板系统 官方文档 一.常用语法 只需要记两种特殊符号: {{  }}和 {% %} ...

  7. Centos7创建CA和申请证书

    转载:http://rackie386.blog.51cto.com/11279229/1947999 Centos7.3创建CA和申请证书 openssl 的配置文件:/etc/pki/tls/op ...

  8. JAVA连接MYSQL,查询 ,添加,删除,语句

        package com; import Java.sql.*;/** *//** * @author Administrator */public class ggg {    private ...

  9. 通过mysql-proxy映射外网访问内网数据库

    配置教程: 转自:http://www.centoscn.com/mysql/2015/0107/4437.html centos安装mysql-proxy mysql-proxy的用处就不再说了 m ...

  10. 基于C语言的Socket网络编程搭建简易的Web服务器(socket实现的内部原理)

    首先编写我们服务器上需要的c文件WebServer.c 涉及到的函数API: int copy(FILE *read_f, FILE * write_f) ----- 文件内容复制的方法 int Do ...