while(!q.empty()){ auto now=q.front();q.pop(); int x=now.first;int y=now.second; inQue[x][y]=false; for(int k=1;k<5;k++){ if(x+dx[k]<0||x+dx[k]>=n||y+dy[k]<0||y+dy[k]>=m)continue;
boolcmp(int a, int b){ return dist[a] < dist[b]; }
classSolution { public: voidSPFA(int src, int n){ for (int i = 0; i <= n; i++) inQue[i] = false, dist[i] = -1; while(!que.empty()) que.pop();
que.push(src); dist[src] = 0; inQue[src] = true; while(!que.empty()){ int x = que.front(); que.pop(); for (PII& e: edge[x]){ int y = e.first, w = e.second; if (dist[y] == -1 || dist[y] > dist[x] + w){ dist[y] = dist[x] + w; if (!inQue[y]) inQue[y] = true, que.push(y); } } inQue[x] = false; } } intcountRestrictedPaths(int n, vector<vector<int>>& edges){ for (int i = 0; i <= n; i++) edge[i].clear(); for (vector<int>& e: edges){ int x = e[0], y = e[1], w = e[2]; edge[x].push_back(MP(y, w)); edge[y].push_back(MP(x, w)); }
SPFA(n, n);
for (int i = 1; i <= n; i++) idx[i] = i, ans[i] = 0; sort(idx + 1, idx + n + 1, cmp);
ans[n] = 1; for (int _ = 1; _ <= n; _++){ int x = idx[_]; if (dist[x] == -1) continue; for (PII& e: edge[x]){ int y = e.first, w = e.second; if (dist[y] <= dist[x]) continue; ans[y] = (ans[y] + ans[x]) % MOD; } }