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;
}

全部评论
链式前向星无向图是要开双倍空间的,邻接表不用
点赞 回复 分享
发布于 2024-08-02 11:13 河南
我也是一模一样,2e5是T了,4e5就过了
点赞 回复 分享
发布于 2024-07-31 20:03 河南
怎么想到开4e5的?我一直优化还是超时,交了10多发
点赞 回复 分享
发布于 2024-07-31 18:05 广东

相关推荐

07-07 12:47
门头沟学院 Java
码农索隆:竟然还真有卡体检报告的
点赞 评论 收藏
分享
大疆在线测评都考什么呀,会考企业概况啥的吗
又被画饼了的做题家很...:不会。刚做完,就是材料分析、态度题、算术题、逻辑题。总共60道。
投递大疆等公司7个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务