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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| #include <iostream> #include <vector> #include <bitset> #include <queue>
using std::cin; using std::cout; using std::cerr; using std::endl; using std::vector;
typedef std::pair<int, int> pii; typedef long long LL;
const int maxN = 1e5 + 2; const int lim = 1e5 + 80; const int mod = 1e9 + 7;
int n, m, k, id, S, T, ans; int pw2[lim + 2]; vector<pii> G[maxN]; std::priority_queue<struct Info> q; std::bitset<maxN> vis;
inline int Mod(int x) { return x >= mod ? x - mod : x < 0 ? x + mod : x; }
namespace SEG { int ncnt; int root[maxN]; int lson[maxN << 7], rson[maxN << 7], val[maxN << 7];
inline void Pushup(int cur, int l, int r) { int mid = (l + r) >> 1; cur[val] = Mod(cur[lson][val] + (LL)cur[rson][val] * pw2[mid - l + 1] % mod); }
void Insert(int& cur, int pre, int l, int r, int pos) { cur = ++ncnt; if (l == r) { cur[val] = 1; return; } int mid = (l + r) >> 1; if (pos <= mid) cur[rson] = pre[rson], Insert(cur[lson], pre[lson], l, mid, pos); else cur[lson] = pre[lson], Insert(cur[rson], pre[rson], mid + 1, r, pos); Pushup(cur, l, r); }
void Update(int& cur, int pre, int l, int r, int x, int y) { if (x <= l and r <= y) { cur = 0; return; } cur = ++ncnt; cur[lson] = pre[lson], cur[rson] = pre[rson], cur[val] = pre[val]; int mid = (l + r) >> 1; if (x <= mid) Update(cur[lson], pre[lson], l, mid, x, y); if (mid < y) Update(cur[rson], pre[rson], mid + 1, r, x, y); Pushup(cur, l, r); }
inline int Query(int cur, int pos) { int l = 0, r = lim, mid; while (l < r) { mid = (l + r) >> 1; if (pos <= mid) cur = cur[lson], r = mid; else cur = cur[rson], l = mid + 1; } return cur[val]; }
inline int _find(int cur, int l, int r) { int mid; while (l < r) { mid = (l + r) >> 1; if (cur[lson][val] == Mod(pw2[mid - l + 1] - 1)) l = mid + 1, cur = cur[rson]; else r = mid, cur = cur[lson]; } return l; }
int Find(int cur, int l, int r, int x, int y) { if (x <= l and r <= y) { if (cur[val] == Mod(pw2[r - l + 1] - 1)) return -1; return _find(cur, l, r); } int mid = (l + r) >> 1, ans = -1; if (x <= mid) ans = Find(cur[lson], l, mid, x, y); if (ans == -1 and mid < y) ans = Find(cur[rson], mid + 1, r, x, y); return ans; }
inline bool Cmp(int x, int y) { int l = 0, r = lim, mid; while (l < r) { mid = (l + r) >> 1; if (x[rson][val] == y[rson][val]) x = lson[x], y = lson[y], r = mid; else x = rson[x], y = rson[y], l = mid + 1; } if (x[val] == y[val]) return x < y; else return x[val] < y[val]; } }
struct Info { int u, root;
Info(int _u, int _root) : u(_u), root(_root) { }
bool operator<(const Info& a) const { return SEG::Cmp(a.root, root); } };
int main() { #ifndef ONLINE_JUDGE freopen("physics.in", "r", stdin); freopen("physics.out", "w", stdout); #endif std::ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> id >> n >> m >> S >> T; pw2[0] = 1; for (int i = 1; i <= lim; ++i) pw2[i] = Mod(pw2[i - 1] << 1); while (m--) { int u, v, w; cin >> u >> v >> w; G[u].emplace_back(v, w), G[v].emplace_back(u, w); } for (int i = 1; i <= n; ++i) if (i != S) SEG::Insert(SEG::root[i], 0, 0, lim, lim); q.push(Info(S, SEG::root[S])); while (!q.empty()) { int u = q.top().u; q.pop(); if (vis[u]) continue; vis.set(u); for (auto i : G[u]) { int v = i.first, w = i.second, tmp = 0, cur = 0; if (SEG::Query(SEG::root[u], w)) { int pos = SEG::Find(SEG::root[u], 0, lim, w, lim); SEG::Update(tmp, SEG::root[u], 0, lim, w, pos - 1); SEG::Insert(cur, tmp, 0, lim, pos); } else SEG::Insert(cur, SEG::root[u], 0, lim, w); if (SEG::Cmp(cur, SEG::root[v])) SEG::root[v] = cur, q.push(Info(v, SEG::root[v])); } } if (!vis[T]) cout << "-1" << endl; else cout << SEG::root[T][SEG::val] << endl; return 0; }
|