1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <bits/stdc++.h> using namespace std;
const int mod = 1e9 + 7; int k;
struct star { int v, nex; } edge[1000 * 2]; int head[50], cnt, n, m;
void ini() { cnt = -1; for (int i = 0; i < n; i++) head[i] = -1; }
void add_edge(int u, int v) { edge[++cnt] = star{v, head[u]}, head[u] = cnt; edge[++cnt] = star{u, head[v]}, head[v] = cnt; }
struct dpnode { int w, ct;
dpnode(int w = 0, int ct = 0) : w(w), ct(ct) {} };
dpnode dp[3][1 << 12][50];
dpnode dpmerge(dpnode a, dpnode b) { return dpnode(a.w + b.w, 1ll * a.ct * b.ct % mod); }
void upd(dpnode &a, dpnode b) { if (b.w < a.w) a = b; else if (b.w == a.w) { a.ct = a.ct + b.ct; if (a.ct >= mod) a.ct -= mod; } }
int main() { while (~scanf("%d%d%d", &n, &m, &k)) { ini(); for (int i = 0, u, v; i < m; i++) { scanf("%d%d", &u, &v); add_edge(u - 1, v - 1); } k--; for (int s = 0; s < 1 << k; s++) { for (int i = 0; i < n; i++) dp[0][s][i] = dp[1][s][i] = dp[2][s][i] = dpnode(1e9, 0); }
for (int i = 0; i < k; i++) { upd(dp[1][1 << i][i], dpnode(0, 1)); upd(dp[0][1 << i][i], dpnode(0, 1)); } for (int s = 1; s < 1 << k; s++) { for (int i = 0; i < n; i++) { for (int s0 = (s - 1) & s; s0; s0 = (s0 - 1) & s) { if ((s0 & -s0) == (s & -s)) {
upd(dp[0][s][i], dpmerge(dp[1][s0][i], dp[0][s ^ s0][i])); } } } struct dijnode { int w,id;
bool operator<(const dijnode &rhs) const { return w > rhs.w; } }; priority_queue<dijnode> q; for (int i = 0; i < n; i++) q.push(dijnode{dp[0][s][i].w, i}); while (!q.empty()) { dijnode top = q.top(); q.pop(); if(top.w!=dp[0][s][top.id].w) continue; for (int i = head[top.id]; ~i; i = edge[i].nex) { if (top.w + 1 <= dp[0][s][edge[i].v].w) { upd(dp[1][s][edge[i].v], dpmerge(dp[0][s][top.id], dpnode(1, 1))); upd(dp[0][s][edge[i].v], dpmerge(dp[0][s][top.id], dpnode(1, 1))); q.push(dijnode{dp[0][s][edge[i].v].w, edge[i].v}); } } } } cout << dp[0][(1 << k) - 1][k].ct+1-1 << endl; } }
|