Morgana is playing a game called End Fantasy VIX. In this game, characters have nn skills, every skill has its damage. And using skill has special condition. Briefly speaking, if this time you use skill "x", then next time you can use skill "y" (just like combo). There are mm conditions (xi, yiy_i), and you can't break the rules. (that means, if you don't have any condition that equals to (xx, yy), then you can't use "y" after use "x").

Now, Morgana wants to defeat the boss, he can use skills t times. In the first time he can use any skill, then he should obey the rules. Besides, he has a special armor called "Xue La", he can use this armor and add a debuff to the boss. The debuff will record damage and when it is over, the record damage will be caused again. (that means double damage) The debuff will continue TT times, and he can use this armor in any time, it won't be in conflict with skills.

Finally, Morgana wants to maximize the damage, but it is too difficult. So please help him deal with this problem.

(If Morgana can not use any skill at a time, he will finish the game and the final damage is his total damage at this time.)

Input

First line contains 44 integers n,m,t,T (2≤n≤642 \le n \le 64, 1≤m≤n×(n−1)1 \le m \le n \times (n-1) , 1≤t≤1e9, 1≤T≤t).

In the next mm lines each line contains two integers represent condition (xi,yix_i, y_i) (xi,yi≤nx_i, y_i \le n) .

Then the next line contains nn integers represent the damage of the skills (every skill's damage is smaller than 1e81e8).

Output

One line with one integer.

思路

题意理解上有些歧义,从“If Morgana can not use any skill at a time, he will finish the game and the final damage is his total damage at this time.”这句理解的话应该是必须满T次才可以翻倍的,但据说场上有clarification说不满T次也可以结算。这篇题解是按照“必须满T次”写的,如果要改成不满T次也可以结算的话就没有必要对矩阵乘法分两种情况讨论了。

最大伤害可能有两种情况,即使用血棘(là)或不使用血辣。使用血辣的情况相当于在一个有向图上选择一条经过点数恰好为T的路径, 将路径上的点权和翻倍,然后在这条路径的首尾加上总数不超过(t-T)的点,使得总点权和最大。不使用血辣的情况则比较简单,直接选择一条经过点数不超过t的路径使得点权和最大即可。

联想到通过邻接矩阵乘法计算有向图上从u到v长度为T的路径条数的思路,不妨尝试将这里的问题转化成可以在矩阵上计算的问题。

用矩阵$A_{ij}$来表示图上从i到j的某些路径的点权和的最大值,如果路径不存在则定义为0。

用“路径合并”运算(即当一条路径的终点与另一条路径的起点均为k时,定义合并的结果为两条路径合并后的点权和)和$\max$运算重定义矩阵乘法:$(A \cdot B)_{ij} = \max_{k}\{A_{ik}\ \mathop{Merge}\ B_{kj}\}$.

由于$\max$运算可交换、可结合、存在单位元0(由于题目中点权均为正数),路径合并运算可结合,且$\max$对“路径合并”满足分配律(即$\max(A_{ik}, B_{ik}) \ \mathop{Merge}\ C_{kj} = \max(A_{ik}\ \mathop{Merge}\ C_{kj},B_{ik}\ \mathop{Merge}\ C_{kj})$),可知重定义后的矩阵乘法是可结合的,即可以用快速幂的思路进行分治计算。

考虑上述运算的实际意义,如果我们将题目给出的有向图写成具有上述性质的矩阵$G$,则$G^{T-1}_{ij}$即为从i到j恰好经过T-1条边(即T个点)的所有路径的最大点权和。这样我们就知道,想要用血辣的话,只要对$G^{T-1}$中的每个元素翻倍就可以了。

最后的问题就是如何在这段使用血辣的路径前后加上总数不超过$(t-T)$的点使得路径长度最大。仍然是用快速幂的思路,我们只要考虑在(t-T)个(G+I)的连乘之间任选一个位置插入刚才翻倍后的$G^{T-1}$,把所有情况用$\max$合并起来即可。

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
int N, M, t, T, v[maxn];
struct Mat
{
LL A[maxn][maxn];
void Print() const
{
for(int i = ;i < N;++i)
for(int j = ;j < N;++j) printf("%lld%c", A[i][j], " \n"[j+ == N]);
puts("");
}
}; bool tp;//因为懒得把运算符重载改成函数所以用了一个全局变量,一般认为这样写是不严谨的
//tp=1时表示路径长度可以小于t
Mat operator + (const Mat &a, const Mat &b)
{
Mat ans;
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
ans.A[i][j] = max(a.A[i][j], b.A[i][j]);
return ans;
} Mat operator * (const Mat &a, const Mat &b)
{
Mat ans;
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
{
if(tp) ans.A[i][j] = max(a.A[i][j], b.A[i][j]);
else ans.A[i][j] = ;
for(int k = ;k < N;++k)
{
if(a.A[i][k] && b.A[k][j])
ans.A[i][j] = max(ans.A[i][j], a.A[i][k] + b.A[k][j] - v[k]);
}
}
return ans;
} Mat G, I;
void init()
{
scanf("%d%d%d%d", &N, &M, &t, &T);
int x, y;
while(M--)
{
scanf("%d%d", &x, &y);
--x, --y;
G.A[x][y] = ;
}
for(int i = ;i < N;++i) scanf("%d", &v[i]);
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
{
if(G.A[i][j] == ) G.A[i][j] = v[i] + v[j];
}
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
I.A[i][j] = ;
for(int i = ;i < N;++i)
I.A[i][i] = v[i];
} Mat powmod(Mat a, int n)
{
Mat ans = I;
while(n)
{
if(n & ) ans = ans * a;
a = a * a;
n >>= ;
}
return ans;
} Mat powmod2(Mat a, Mat g, int n)
{
Mat ans = a, pw = I;
a = a * g + g * a;
while(n)
{
if(n & )
{
ans = ans + pw * a + a * pw;
pw = pw * g + g * pw;
}
n >>= ;
a = a * g + g * a;
g = g * g;
}
return ans;
} void work()
{
tp = false;//必须够T次
Mat a = powmod(G, T-);
bool useXL = false;
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
if(a.A[i][j])
{
useXL = true;
a.A[i][j] <<= ;
}
LL ans = ;
tp = true;//可以不足t次
if(useXL)
{
a = powmod2(a, G, t - T);
for(int i = ;i < N;++i) for(int j = ;j < N;++j) ans = max(ans, a.A[i][j]);
} a = powmod(G, t);
for(int i = ;i < N;++i) for(int j = ;j < N;++j) ans = max(ans, a.A[i][j]);
printf("%lld\n", ans);
} int main()
{
init();
work();
return ;
}

矩阵运算推广

 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
int N, M, t, T, v[maxn];
struct Mat
{
LL A[maxn][maxn];
void Print() const
{
for(int i = ;i < N;++i)
for(int j = ;j < N;++j) printf("%lld%c", A[i][j], " \n"[j+ == N]);
puts("");
}
}; Mat operator + (const Mat &a, const Mat &b)
{
Mat ans;
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
ans.A[i][j] = max(a.A[i][j], b.A[i][j]);
return ans;
} Mat operator * (const Mat &a, const Mat &b)
{
Mat ans;
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
{
ans.A[i][j] = max(a.A[i][j], b.A[i][j]);
for(int k = ;k < N;++k)
{
if(a.A[i][k] && b.A[k][j])
ans.A[i][j] = max(ans.A[i][j], a.A[i][k] + b.A[k][j] - v[k]);
}
}
return ans;
} Mat G, I;
void init()
{
scanf("%d%d%d%d", &N, &M, &t, &T);
int x, y;
while(M--)
{
scanf("%d%d", &x, &y);
--x, --y;
G.A[x][y] = ;
}
for(int i = ;i < N;++i) scanf("%d", &v[i]);
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
{
if(G.A[i][j] == ) G.A[i][j] = v[i] + v[j];
}
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
I.A[i][j] = ;
for(int i = ;i < N;++i)
I.A[i][i] = v[i];
} Mat powmod(Mat a, int n)
{
Mat ans = I;
while(n)
{
if(n & ) ans = ans * a;
a = a * a;
n >>= ;
}
return ans;
} Mat powmod2(Mat a, Mat g, int n)
{
Mat ans = a, pw = I;
a = a * g + g * a;
while(n)
{
if(n & )
{
ans = ans + pw * a + a * pw;
pw = pw * g + g * pw;
}
n >>= ;
a = a * g + g * a;
g = g * g;
}
return ans;
} void work()
{
Mat a = powmod(G, T-);
bool useXL = false;
for(int i = ;i < N;++i) for(int j = ;j < N;++j)
if(a.A[i][j])
{
useXL = true;
a.A[i][j] <<= ;
}
LL ans = ;
if(useXL)
{
a = powmod2(a, G, t - T);
for(int i = ;i < N;++i) for(int j = ;j < N;++j) ans = max(ans, a.A[i][j]);
} a = powmod(G, t);
for(int i = ;i < N;++i) for(int j = ;j < N;++j) ans = max(ans, a.A[i][j]);
printf("%lld\n", ans);
} int main()
{
init();
work();
return ;
}

不满T个也可以翻倍的版本

 

【ACM-ICPC 2018 徐州赛区网络预赛】E. End Fantasy VIX 血辣 (矩阵运算的推广)的更多相关文章

  1. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  2. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

    ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...

  3. 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)

    H.Ryuji doesn't want to study 27.34% 1000ms 262144K   Ryuji is not a good student, and he doesn't wa ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)

    传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...

  5. ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE

    In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...

  6. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study

    262144K   Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 F. Features Track

    262144K   Morgana is learning computer vision, and he likes cats, too. One day he wants to find the ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash

    Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encryp ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和

    链接 https://nanti.jisuanke.com/t/31456 参考题解  https://blog.csdn.net/ftx456789/article/details/82590044 ...

  10. ACM-ICPC 2018 徐州赛区网络预赛(8/11)

    ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 枚举第一个选的,接下来的那个不能取前一个的取反 \(DP[i][0]\)表示选和第一个相同的 \(DP[i][1]\) ...

随机推荐

  1. Servlet笔记3--Servlet生命周期

    Servlet生命周期:

  2. WindowsServer2003双网卡配置

    今天突然被问起,找资料10+分钟才找到记录,因此再次记录下: route -p delete 0.0.0.0 route -p add 0.0.0.0 mask 0.0.0.0 58.240.115. ...

  3. bind系统调用

    /* * Bind a name to a socket. Nothing much to do here since it's * the protocol's responsibility to ...

  4. Resouce, platform_device 和 platform_driver 的关系【转】

    转自:http://blog.csdn.net/uruita/article/details/7278313 從2.6版本開始引入了platform這個概念,在開發底層驅動程序時,首先要確認的就是設備 ...

  5. 如何开启mysql5.5的客户端服务 命令行打开方法

    MySQL分为两个部分,服务器端和客户端,只有服务器端的服务开启后,才可以通过客户端登录到MySQL数据库.这里介绍如何用命令行方式开启mysql的客户端服务. 在计算机上安装好mysql软件   我 ...

  6. Jenkins关联GitHub进行构建

    一.创建一个自由风格的项目 并在高级中勾选你构建完成后保存项目的路径 二.配置你存放代码的GitHub的地址并添加用户名密码 三.立即构建

  7. Java集合Map与其子类回顾

    接10月12号昨天的笔记,今天继续回顾集合中的Map集合. 一.集合工具操作类Collections 问题:collection和collections的区别? 1.collection是单列集合的顶 ...

  8. RabbitMQ--Publish/Subscribe(三)

    前言 在先前例子中我们创建了一个work queue,封装一个task到一个message中并发送到queue.一个worker(consumer)取出任务并执行. 像这种producer把messa ...

  9. java基础59 JavaScript运算符与控制流程语句(网页知识)

    1.JavaScript运算符 1.1.加减乘除法 加法:+(加法,连接符,正数)          true是1,false是0    减法:-    乘法:*    除法:/ 1.2.比较运算符 ...

  10. 2016-2017-2 20155309南皓芯《java程序设计》第七周学习总结

    教材学习内容总结 Lambda 一种匿名方法 表达式构成 括号以及括号里用逗号分隔的参数列表 仅有一个参数的可以省略括号 ->符号 花括号以及花括号里的语句 仅有一条语句时可以省略花括号,并且这 ...