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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll Mod = 1e9+7; ll sum(ll a, ll b) { return (a + b) % Mod; } ll& Add(ll &a, ll b) { return a = sum(a, b); } ll product(ll a, ll b) { return a * b % Mod; } ll& Mul(ll &a, ll b) { return a = product(a, b); } const ll iv = 570000004LL;
ll talkative[1 << 17], ti[17], pi[20], mi[17]; int V, E, S, F, n; const int N = 1020; const ll INF = 253432145421354LL; ll sp[20][20], d[N]; struct edge{ int to; ll cost; edge(int to = 0, ll cost = 0):to(to), cost(cost){} }; vector<edge> G[N]; int studentOnVertex[N]; void Mini(ll &a, ll b) { if (b < a) a = b; } typedef pair<ll, int> P; typedef pair<ll, P> Tuple; ll dp[1<<17][20], dp2[1<<17];
int main() { scanf("%d%d%d%d%d", &V, &E, &S, &F, &n); S--, F--; for (int i = 0; i < n; i++) { scanf("%lld%lld%lld", pi + i, ti + i, mi + i); pi[i]--; } for (int i = 0; i < E; i++) { int x,y,w; scanf("%d%d%d", &x, &y, &w); --x,--y; G[x].push_back(edge(y, w)); G[y].push_back(edge(x, w)); } talkative[0] = 1; for (int x = 0; x < (1 << n); x++) { for (int i = 0; i < n; i++) { if ((x & (1 << i)) == 0) { talkative[x|(1<<i)] = product(talkative[x], ti[i]); } } } memset(studentOnVertex, -1, sizeof studentOnVertex); for (int i = 0; i < n; i++) { studentOnVertex[pi[i]] = i; } pi[n] = S; pi[n + 1] = F;
for (int s = 0; s <= n + 1; s++) for (int t = s + 1; t <= n + 1; t++) { fill(d, d + V, INF); d[pi[s]] = 0; priority_queue<P, vector<P>, greater<P> > que; que.push(P(0, pi[s])); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (p.first > d[v]) continue; for (auto e: G[v]) { int u = e.to; ll cost = e.cost; int stui = studentOnVertex[u]; if (stui != -1 && stui != s && stui != t) continue; if (d[u] > d[v] + cost) { d[u] = d[v] + cost; que.push(P(d[u], u)); } } } sp[s][t] = sp[t][s] = d[pi[t]]; }
for (int x = 0; x < (1 << n); x++) for (int i = 0; i <= n; i++) dp[x][i] = INF; dp[0][n] = 0; priority_queue<Tuple, vector<Tuple>, greater<Tuple> > que; que.push(Tuple(0, P(0,n)));
while (!que.empty()) { Tuple tup = que.top(); que.pop(); int x = tup.second.first, i = tup.second.second; if (tup.first > dp[x][i]) continue; for (int j = 0; j < n; j++) { if (j == i) continue; int nx = x | (1 << j); if (dp[nx][j] > dp[x][i] + sp[i][j]) { dp[nx][j] = dp[x][i] + sp[i][j]; que.push(Tuple(dp[nx][j], P(nx,j))); } } } for (int x = 1; x < (1 << n); x++) { dp2[x] = INF; for (int i = 0; i < n; i++) { if (x & (1 << i)) Mini(dp2[x], dp[x][i] + sp[i][n+1]); } dp2[x] += talkative[x]; }
for (int x = (1 << n) - 1; x > 0; x--) { for (int i = 0; i < n; i++) Mini(dp2[x], dp2[x|(1<<i)]); } ll ans = 0; for (int x = 1; x < (1 << n); x++) { ll temp = 1; for (int i = 0; i < n; i++) { if (x & (1 << i)) { Mul(temp, mi[i]); } else { Mul(temp, 100 - mi[i]); } Mul(temp, iv); } Mul(temp, dp2[x]); Add(ans, temp); } printf("%lld\n", ans); }
|