码迷,mamicode.com
首页 > Web开发 > 详细

SSSP dijstra+stl::heap 邻接表模版

时间:2019-07-12 13:06:35      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:dig   sdi   def   return   struct   define   priority   nod   const   

//SSSP dijstra+stl::heap 邻接表模版 
#include<bits/stdc++.h>
using namespace std;
#define why 105
#define whym 1455
#define inf 0x3f3f3f3f
int n,m,d[why],h[why],cnt,s,t;
bool v[why];
struct node
{
    int next,to,v;
}a[whym*2];
struct tap
{
    int num,v;
    bool operator <(const tap &x)const
    {
        return x.num<num;
    }
};
inline int redn()
{
    int ret=0,f=1;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch==-)f=-f;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        ret=ret*10+ch-0;
        ch=getchar();
    }
    return f>0?ret:-ret;
}
inline void dij(int s)
{
    priority_queue<tap>q;
    tap k;
    d[s]=0;
    q.push((tap){0,s});
    while(!q.empty())
    {
        k=q.top();
        q.pop();
        if(v[k.v])continue;
        v[k.v]=1;
        for(int u=h[k.v];u;u=a[u].next)
        {
            int j=a[u].to;
            if(d[j]>d[k.v]+a[u].v)
            {
                d[j]=d[k.v]+a[u].v;
                if(!v[j])q.push((tap){d[j],j});
            }
        }
    }
}
inline void DoubleAdd(int x,int y,int z)
{
    a[++cnt].next=h[x];
    a[cnt].to=y;
    a[cnt].v=z;
    h[x]=cnt;
    a[++cnt].next=h[y];
    a[cnt].to=x;
    a[cnt].v=z;
    h[y]=cnt;
}
int main()
{
    int _1,_2,_3;
    register int i;
    n=redn();
    m=redn();
    s=redn();
    t=redn();
    memset(d,0x3f,sizeof(d));
    for(i=1;i<=m;i++)
    {
        _1=redn(),_2=redn(),_3=redn();
        DoubleAdd(_1,_2,_3);
    }
    dij(s);
    printf("%d",d[t]);
    return 0;
}

 

SSSP dijstra+stl::heap 邻接表模版

标签:dig   sdi   def   return   struct   define   priority   nod   const   

原文地址:https://www.cnblogs.com/NOI-AKer/p/11175280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!