I题为啥空间开2e5过不了呀?
最后开了4e5才过,可m不是≤1e5的吗?
#include <iostream> #include <cstring> #include <algorithm> #include <queue> #define x first #define y second typedef long long LL; using namespace std; typedef pair<LL, int> PII; const int N = 2e5 + 10; const LL INF = 1e18; int h[N], e[N], ne[N], c[N], d[N], idx; int n, m, k; LL dist[2][N]; bool st[N]; void add(int a, int b, int c0, int d0) { e[idx] = b, c[idx] = c0, d[idx] = d0, ne[idx] = h[a], h[a] = idx++; } void dijkstra(int u, int type, LL dist[]) { priority_queue<PII, vector<PII>, greater<PII>> heap; for (int i = 1; i <= n; i++) dist[i] = INF, st[i] = 0; dist[u] = 0; heap.push({0, u}); while (heap.size()) { PII t = heap.top(); heap.pop(); int ver = t.y; LL distance = t.x; if (st[ver]) continue; st[ver] = 1; for (int i = h[ver]; ~i; i = ne[i]) { int j = e[i]; if (!type && !d[i]) continue; if (dist[j] > distance + c[i]) { dist[j] = distance + c[i]; heap.push({dist[j], j}); } } } } int main() { memset(h, -1, sizeof h); scanf("%d%d%d", &n, &m, &k); while (m--) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); add(a, b, c, d), add(b, a, c, d); } dijkstra(1, 0, dist[0]); if (dist[0][n] == INF && dist[0][k] == INF) { puts("-1"); return 0; } dijkstra(k, 1, dist[1]); if (dist[0][n] == INF && dist[1][n] == INF) { puts("-1"); return 0; } printf("%lld\n", min(dist[0][n], dist[0][k] + dist[1][n])); return 0; }