题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=1875

题解

如果没有这个“不能立刻沿着刚刚走来的路走回”,那么这个题就是一个常规的矩阵乘法。

考虑一下这个限制怎么解决。因为限制的是边,我们不妨考虑和边有关的矩阵。

首先把一条无向边拆成两个有向边,如果边 \(A\) 的终点和边 \(B\) 的起点相同,那么我们就说从边 \(A\) 通向边 \(B\)。但是,同源的有向边(也就是从同一条无向边拆成的两条有向边)之间不能建边。

但是为了能够区分出来从 \(S\) 到 \(T\) 的路径,我们建立两个虚点,从 \(SS\) 到 \(S\) 的一条边和从 \(T\) 到 \(TT\) 的边。

然后我们把这个东西跑一边矩阵快速幂即可。


时间复杂度 \(O(m^3\log n)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 120 + 4;
const int P = 45989; int n, m, T, st, ed;
char s[N]; struct Edge { int to, ne; } g[N << 1]; int head[N], tot = 1;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); } inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = x * x % P) if (y & 1) ans = ans * x % P;
return ans;
} struct Matrix {
int a[N][N]; inline Matrix() { memset(a, 0, sizeof(a)); }
inline Matrix(const int &x) {
memset(a, 0, sizeof(a));
for (int i = 1; i <= m; ++i) a[i][i] = x;
} inline Matrix operator * (const Matrix &b) {
Matrix c;
for (int k = 1; k <= m; ++k)
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= m; ++j)
sadd(c.a[i][j], a[i][k] * b.a[k][j] % P);
return c;
}
} A; inline Matrix fpow(Matrix x, int y) {
Matrix ans(1);
for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
return ans;
}
inline void work() {
addedge(++n, st), addedge(ed, ++n), m = tot;
for (int x = 0; x <= n; ++x)
for fec(i, x, y) for fec(j, y, z) if ((i ^ j) != 1) A.a[i][j] = 1;
A = fpow(A, T + 1);
printf("%d\n", A.a[m - 1][m]);
} inline void init() {
read(n), read(m), read(T), read(st), read(ed);
int x, y;
for (int i = 1; i <= m; ++i) read(x), read(y), adde(x, y);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

最新文章

  1. ios使用CocoaHTTPServer实现文件共享
  2. iOS:使用代理模式监听开关状态改变事件
  3. asp.net LINQ防止SQL注入式攻击
  4. windows下在文件夹中快速启动cmd
  5. PHP工厂模式的研究
  6. LeetCode Reverse Words in a String II
  7. CSS布局一
  8. HDU 4882 ZCC Loves Codefires (贪心)
  9. mysql查询更新时的锁表机制分析
  10. iOS 非ARC基本内存管理系列 4-autorelease方法和@autoreleasepool
  11. iOS Runtime 实践(1)
  12. POJ2566-Bound Found (尺取法)
  13. 移动web页面给用户发送邮件的方法
  14. Linux lvs三种模式工作原理
  15. Linux查看端口占用情况并释放端口占用
  16. 使用jquery获取iframe内的元素属性
  17. day 09 初识函数
  18. Pandas的使用(2)
  19. you-get
  20. python短信发送

热门文章

  1. package.json保存
  2. electron原来这么简单----打包你的react、VUE桌面应用程序
  3. Microsoft Visual Studio 2013 Language Pack
  4. HTML - form 表单提交
  5. python-异常处理总结
  6. Linux_文件系统&磁盘分区
  7. javascript实现保留两位小数的多种方法
  8. 【ABAP系列】SAP ABAP同时显示多个ALV的方法
  9. C# DropDownList绑定添加新数据的三种方法
  10. 防止XSRF 解决方案