同样都是dijkstra + heap优化,我的28pts 别人的100pts
查看原帖
同样都是dijkstra + heap优化,我的28pts 别人的100pts
1026350
Suboil楼主2023/9/5 09:21

我写的

// void dijkstra() {
//     memset(d,0x3f,sizeof(d));
//     priority_queue<int,vector<int>,greater<int> > heap;
//     d[s] = 0;
//     heap.push(s);
//     while(heap.size()) {
//         int fr = heap.top();
//         heap.pop();
//         if(st[fr]) continue;
//         st[fr] = 1;
//         for(int i = h[fr];i;i = e[i].ne) {
//             int j = e[i].to,w = e[i].w;
//             if(d[j] > d[fr] + w) {
//                 d[j] = d[fr] + w;
//                 heap.push(j);
//             }
//         }
//     }
// }

别人写的


void dijkstra(int s)
{
    memset(d,0x3f,sizeof(d));
    d[s]=0;
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > points;
    points.push(make_pair(0,s));
    while(!points.empty())
    {
        int u=points.top().second;
        points.pop();
        if(!st[u])
        {
            st[u]=1;
            for(int i=h[u];i;i=e[i].ne)
            {
                int to=e[i].to;
                if(d[to]>d[u]+e[i].w) 
                {
                    d[to]=d[u]+e[i].w;
                    points.push(make_pair(d[to],to));
                }
            }
        }
    }
}
2023/9/5 09:21
加载中...