当前位置:网站首页>Heap optimization dijkstra/hash table storage node number

Heap optimization dijkstra/hash table storage node number

2022-06-26 14:24:00 Honestbutter-

link

  1. The data range of node number has reached 1 e 18 1e18 1e18, Through hash table M A P < L L , i n t > m p MAP<LL,int>mp MAP<LL,int>mp Optimize . use g e t ( ) get() get() Function to convert .
  2. Note that the shortest circuit length reaches l o n g l o n g long long longlong Level , therefore d [ ] d[] d[] For array initialization f o r for for The loop is initialized to 1 e 13 1e13 1e13
#include<iostream>
#include<queue>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=4e5+100;
typedef long long LL;
typedef pair<LL,int> PLI;
int e[M],ne[M],w[M],h[M],idx;
LL d[M];
map<LL,int>has;
bool st[M];
LL m,xa,xb,s;
LL a,b,l,t;
int n;
void add(LL a,LL b,LL c)
{
    
    e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}   
int get(LL x)
{
    
    if(has.count(x)) return has[x];
    
    has[x]=++n;
    return has[x];
}
LL dijkstra()
{
    
    priority_queue<PLI,vector<PLI>,greater<PLI>>q;
    q.push({
    0,xa});
    
// memset(d,0x3f,sizeof d);
    for(int i=1;i<=n;i++) d[i]=1e13;
    d[xa]=0;
    
    while(!q.empty())
    {
    
        auto t=q.top();
        q.pop();
        
        int sed=t.second;
        if(!st[sed])
        {
    
            st[sed]=true;
            for(int i=h[sed];i!=-1;i=ne[i])
            {
    
                int j=e[i];
                if(d[j]>d[sed]+w[i])
                {
    
                    d[j]=d[sed]+w[i];
                    q.push({
    d[j],j});
                }
            }
        }
    }
    
    if(d[xb]==1e13)  return -1;
    return d[xb];
}
int main()
{
    
    scanf("%lld%lld%lld%lld",&m,&xa,&xb,&s);
    xa=get(xa),xb=get(xb);
    
    memset(h,-1,sizeof h);
    while(m--)
    {
    
        scanf("%lld%lld%lld%lld",&a,&b,&l,&t);
        a=get(a),b=get(b);
        add(a,b,l);
        if(t==2) add(b,a,l);
    }
    
    LL ans=dijkstra();
    
    if(ans==-1) cout<<-1<<endl;
    else cout<<(ans+s-1)/s<<endl;
    return 0;
}
原网站

版权声明
本文为[Honestbutter-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170508103354.html