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. jquery记忆笔记

    1.javascript需要注意的一些问题: ①不要使用==比较,始终坚持使用===比较. false == 0; // true false === 0; // false ②NaN这个特殊的Num ...

  2. mysql高可用架构 -> MHA部署-04

    MHA架构图 本次MHA的部署基于GTID复制成功构建,普通主从复制也可以构建MHA架构. 下载所需的软件包 mkdir /server/tools -p //创建存放包的目录 [root@db01 ...

  3. linux快速安装mysql教程

    #安装mysql服务器:yum install mysql-server #设置开机启动chkconfig mysqld on#现在启动服务service mysqld start #设置root初始 ...

  4. jQuery之字体大小的设置

    先获取字体大小,进行处理. 再将修改的值保存. slice() 方法可从已有的数组中返回选定的元素.arrayObject.slice(start,end).start     必需.规定从何处开始选 ...

  5. 洛谷P1120 小木棍(升级版)

    传送门啦 一道经典的搜索剪枝题,不废话,步入正题. 分析: 一.输入时手动过滤不合法的情况 二.很明显我们要枚举把哪些棍子拼接成原来的长棍,而原始长度(原来的长棍的长度)都相等,因此我们可以在 $ d ...

  6. LeetCode699. Falling Squares

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  7. » Working Around JNI UTF-8 Strings Deprogramming

    private static native void printString(String text); ... void examplePrintString() { String str = &q ...

  8. 20165203&20165206结对创意感想

    一.结对学习过程 我和我的搭档性格志趣相投,而且各有所长,我们两个均属于一丝不苟的人,做一件事就要把它做好.因此,我们学习理念相同,志趣相投,这可能会占很大的优势.首先,我们会利用一周的前几天看课本, ...

  9. pycharm、webstorm和idea激活码

    pycharm ---> http://blog.csdn.net/kevinelstri/article/details/57413791 idea ---> http://idea.l ...

  10. python类、类继承

    yield: 简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator,调用 fab( ...