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
| #include <cstdio> #include <queue> #include <iostream> #include <cmath> #define int long long
using namespace std;
const int maxn = 1000005;
int head[maxn]; int ecnt = 2, n, m; double dis[maxn]; int pre[maxn]; int pre_weight[maxn]; bool vis[maxn];
struct edge { int to, next, w; } g[maxn << 1]; void add_edge(int u, int v, int w) { g[ecnt] = (edge) { v, head[u], w }; head[u] = ecnt ++; }
struct node { double dis, pos; friend bool operator < (const node &a, const node &b) { return a.dis > b.dis; } }; priority_queue<node> q;
void dijkstra() { for (int i = 1; i <= n; i ++) dis[i] = 8e18; dis[1] = 0; q.push((node) { 0, 1 }); while (!q.empty()) { int u = q.top().pos; q.pop(); if (vis[u]) continue; vis[u] = true; for (int e = head[u]; e != 0; e = g[e].next) { int v = g[e].to; if (dis[v] > dis[u] + log2(g[e].w)) { dis[v] = dis[u] + log2(g[e].w); q.push((node) { dis[v], v }); pre[v] = u; pre_weight[v] = g[e].w; } } } }
signed main() { cin >> n >> m; for (int i = 1; i <= m; i ++) { int x, y, z; cin >> x >> y >> z; add_edge(x, y, z); if (z == 0) { cout << 0 << endl; return 0; } } dijkstra(); int i = n; int ans = 1; while (pre[i] != 0) { ans = ans * pre_weight[i]; ans = ans % 9987; i = pre[i]; } cout << ans << endl; }
|