- f[i][S]三进制压缩表示最长路为i,0代表不在该集合,1代表不是最短路为i集合,2代表是最短路集合, 转移枚举i+1集合是那些, 乘以概率即可
- 预处理保证复杂度
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define ll long long
#define mmp make_pair
#define M 13
#define N 610000
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
const int mod = 998244353;
void add(int &x, int y) {
x += y;
x -= x >= mod ? mod : 0;
}
int mul(int a, int b) {
return 1ll * a * b % mod;
}
int poww(int a, int b) {
int ans = 1, tmp = a;
for(; b; b >>= 1, tmp = mul(tmp, tmp)) if(b & 1) ans = mul(ans, tmp);
return ans;
}
int n, k, note[M][M], q[4100][4100], f[M][N], g[N], h[N], u, pw[M], cnt;
int main() {
n = read(), k = read();
u = (1 << (n - 1)) - 1;
for(int i = 0; i <= n; i++) pw[i] = (1 << i);
for(int i = 1; i <= (n * (n - 1)); i++) {
int vi = read(), vj = read(), p = read(), q = read();
note[vi][vj] = mul(p, poww(q, mod - 2));
}
for(int i = 0; i <= u; i++) {
for(int j = 0; j <= u; j++) {
if((i | j) == i) {
q[i][j] = ++cnt;
int s = (i ^ j), t = j;
g[cnt] = h[cnt] = 1;
for(int r = 2; r <= n; ++r)
if(t & pw[r - 2]) {
int p = 1;
for(int l = 2; l <= n; l++) {
if(s & pw[l - 2]) p = mul(p, (1 - note[l][r] + mod) % mod);
}
g[cnt] = mul(g[cnt], (1 - p + mod));
h[cnt] = mul(h[cnt], p);
h[cnt] = mul(h[cnt], (1 - note[1][r] + mod));
}
}
}
}
for(int s = 0; s <= u; s++) {
f[1][q[s][s]] = 1;
for(int i = 2; i <= n; i++) if(s & pw[i - 2]) f[1][q[s][s]] = mul(f[1][q[s][s]], note[1][i]);
}
for(int i = 1; i < k; i++) {
for(int s = 0; s <= u; s++) {
for(int now = s; now; now = (now - 1) & s) {
if(!f[i][q[s][now]]) continue;
for(int t = (u ^ s); t; t = (t - 1) & (u ^ s)) {
add(f[i + 1][q[s | t][t]], mul(mul(f[i][q[s][now]], g[q[now | t][t]]), h[q[(now ^ s) | t][t]]));
}
}
}
}
int ans = 0;
for(int i = 1; i <= k; i++)
for(int s = 0; s <= u; s++) add(ans, f[i][q[u][s]]);
cout << ans <<"\n";
return 0;
}