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

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.

思路

最大伤害可能有两种情况,即使用血棘(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}$中的每个元素翻倍就可以了。

待续

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4 const int maxn = 64;
  5 int N, M, t, T, v[maxn];
  6 struct Mat
  7 {
  8     LL A[maxn][maxn];
  9     void Print() const
 10     {
 11         for(int i = 0;i < N;++i)
 12             for(int j = 0;j < N;++j) printf("%lld%c", A[i][j], " \n"[j+1 == N]);
 13         puts("");
 14     }
 15 };
 16
 17 bool tp;//因为懒得把运算符重载改成函数所以用了一个全局变量,一般认为这样写是不严谨的
 18         //tp=1时表示路径长度可以小于t
 19 Mat operator + (const Mat &a, const Mat &b)
 20 {
 21     Mat ans;
 22     for(int i = 0;i < N;++i) for(int j = 0;j < N;++j)
 23         ans.A[i][j] = max(a.A[i][j], b.A[i][j]);
 24     return ans;
 25 }
 26
 27 Mat operator * (const Mat &a, const Mat &b)
 28 {
 29     Mat ans;
 30     for(int i = 0;i < N;++i) for(int j = 0;j < N;++j)
 31     {
 32         if(tp) ans.A[i][j] = max(a.A[i][j], b.A[i][j]);
 33         else ans.A[i][j] = 0;
 34         for(int k = 0;k < N;++k)
 35         {
 36             if(a.A[i][k] && b.A[k][j])
 37                 ans.A[i][j] = max(ans.A[i][j], a.A[i][k] + b.A[k][j] - v[k]);
 38         }
 39     }
 40     return ans;
 41 }
 42
 43 Mat G, I;
 44 void init()
 45 {
 46     scanf("%d%d%d%d", &N, &M, &t, &T);
 47     int x, y;
 48     while(M--)
 49     {
 50         scanf("%d%d", &x, &y);
 51         --x, --y;
 52         G.A[x][y] = 1;
 53     }
 54     for(int i = 0;i < N;++i) scanf("%d", &v[i]);
 55     for(int i = 0;i < N;++i) for(int j = 0;j < N;++j)
 56     {
 57         if(G.A[i][j] == 1) G.A[i][j] = v[i] + v[j];
 58     }
 59     for(int i = 0;i < N;++i) for(int j = 0;j < N;++j)
 60         I.A[i][j] = 0;
 61     for(int i = 0;i < N;++i)
 62         I.A[i][i] = v[i];
 63 }
 64
 65 Mat powmod(Mat a, int n)
 66 {
 67     Mat ans = I;
 68     while(n)
 69     {
 70         if(n & 1) ans = ans * a;
 71         a = a * a;
 72         n >>= 1;
 73     }
 74     return ans;
 75 }
 76
 77 Mat powmod2(Mat a, Mat g, int n)
 78 {
 79     a = a * g + g * a;
 80     Mat ans = a, pw = I;
 81     while(n)
 82     {
 83         if(n & 1)
 84         {
 85             ans = ans + pw * a + a * pw;
 86             pw = pw * g + g * pw;
 87         }
 88         n >>= 1;
 89         a = a * g + g * a;
 90         g = g * g;
 91     }
 92     return ans;
 93 }
 94
 95 void work()
 96 {
 97     tp = false;//必须够T次
 98     Mat a = powmod(G, T-1);
 99     bool useXL = false;
100     for(int i = 0;i < N;++i) for(int j = 0;j < N;++j)
101         if(a.A[i][j])
102         {
103             useXL = true;
104             a.A[i][j] <<= 1;
105         }
106     LL ans = 0;
107     tp = true;//可以不足t次
108     if(useXL)
109     {
110         a = powmod2(a, G, t - T);
111         for(int i = 0;i < N;++i) for(int j = 0;j < N;++j) ans = max(ans, a.A[i][j]);
112     }
113
114     a = powmod(G, t);
115     for(int i = 0;i < N;++i) for(int j = 0;j < N;++j) ans = max(ans, a.A[i][j]);
116     printf("%lld\n", ans);
117 }
118
119 int main()
120 {
121     init();
122     work();
123     return 0;
124 }

矩阵运算推广

原文地址:https://www.cnblogs.com/Asm-Definer/p/9615562.html

时间: 2024-11-07 08:51:01

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

ACM-ICPC 2018 徐州赛区网络预赛 D. EasyMath

ACM-ICPC 2018 徐州赛区网络预赛 D. EasyMath 做法: \[f(m,n) = \sum _{i=1}^{m} \mu(in) = \sum_{i=1}^{m}[gcd(i,n)=1]\mu(i)\mu(n) = \mu(n)\sum_{d|n}\mu(d)f(\frac{m}{d},d)\] 边界: n=1,杜教筛求\(\sum_{i=1}^{m}\mu(i)\),m = 1, 返回\(\mu(n)\),预处理尽可能把空间卡满. 2个小时的时候就推出来了这个式子,不会算复杂

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;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 learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he wi

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;t want to study (线段树)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he will get a

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

In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named "Sena" are playing a video game. The game system of this video game is quite unique: in the process of playing this game, you need to constantly fac

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

ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle. The map consists of N \times MN×M little squares. That is to say, the h

ICPC 2018 徐州赛区网络赛

ACM-ICPC 2018 徐州赛区网络赛 ?去年博客记录过这场比赛经历:该死的水题 ?一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. ? ? D. Easy Math 题意: ? 给定 \(n\), \(m\) ,求 \(\sum _{i=1}^{m} \mu(in)\) .其中 $ 1 \le n \le 1e12$ , $ 1 \le m \le 2e9$ ,\(\mu(n)\) 为莫比乌斯函数. ? 思路: ? 容易知道,\(i\) 与 \(n\) 不互质时, \(\m

ACM-ICPC 2018 徐州赛区网络预赛

Rank Solved A B C D E F G H I J K 157/1526 7/11 O O ? ? . O O O O O ? O: 当场通过 ?: 赛后通过 .: 尚未通过 A Hard to prepare solved by chelly chelly's solution B BE, GE or NE solved by ch ch's solution C Cacti Lottery upsolved by chelly chelly's solution 考虑枚举*代表的

ACM-ICPC 2018 徐州赛区网络预赛 A. Hard to prepare

传送门:https://nanti.jisuanke.com/t/31453 本题是一个组合数学(DP,滑稽)题. 一个环上有N个位置,标号为1~N.设第i(1≤i≤N)个位置上的数为x[i],限制条件为:0≤x[i]<2k.另有限制条件:当位置i和位置j相邻时,x[i]⊕x[j]≠2k-1.求满足限制条件的环的状态数. 可以考虑将环切割成链,以分析问题.设: ①a[n]:长度为n,且首尾相同的满足上述条件的链的状态数: ②b[n]:长度为n,且首尾相反的满足上述条件的链的状态数: ③c[n]:

ACM-ICPC 2018 徐州赛区网络预赛(9.9)

#include<bits/stdc++.h> #define int long long using namespace std; const int maxn=1e6+10; const int mod=1e9+7; int dp[maxn][3]; int quick(int a,int n) { int ans=1; int x=a; while(n!=0) { if(n%2==1) {ans=ans%mod*x%mod; n--; } else { x=x%mod*x%mod;n=n