大佬们,为啥我的代码会TLE呀,真不知道咋改了

P4779 【模板】单源最短路径(标准版)

dududu2024 @ 2024-02-01 21:52:10

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 100010;
int n , m , s, u, v, w1;
int h[N],e[N],ne[N],w[N],idx;
int dist[N];
bool st [N];
typedef pair<int , int> PII;
void add(int x,int y ,int c)//邻接表
{
    e[idx] = y;
    w[idx] = c;
    ne[idx] = h[x];
    h[x] = idx ++;
}

void dijkstr ()
{
    dist[1] = 0;

    priority_queue<PII,vector<PII>,greater<PII>> heap;
    heap.push({0,1});

    while(heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second, distance = t.first;

        if(st[ver]) continue;
        st[ver] = true;

        for(int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > dist [ver] + w[i])
            {
                dist[j] = dist [ver] + w[i];
                heap.push({dist[j],j});
            }
        }
    }

}
int main ()
{
    cin >> n >> m >> s;
    memset(dist,0x3f,sizeof dist);
    memset(h,-1,sizeof h);
    while(m--)
    {
        scanf("%d%d%d ",&u,&v,&w1);
        add(u,v,w1);
    }
    dijkstr();
    for(int i = 1; i <= n;i++)
    {
        printf("%d ",dist[i]);
    }
    printf("\n");

    return 0;
}

by dududu2024 @ 2024-02-01 21:53:07

明明已经 使用 优先队列进行优化了


by lastxuans @ 2024-02-15 00:55:21

你数组开小了


by Hhy882577 @ 2024-02-20 20:42:02

确实,题目m都是2*10^5,你这100010根本不够


|