zoukankan      html  css  js  c++  java
  • dijk的堆优化(链式前向星存图)(结构体运算符重载)(小根堆对结构体的排序)

    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<iostream>
    #include<list>
    #include<stack>
    #include<deque>
    #include<cstring>
    using namespace std;
    struct node
    {
        int to;
        int w;
        int nex;
        bool operator > (const node temp) const
        {
            return w>temp.w;
        }
    }a[10005];
    int first[10005];
    int dis[10005];
    int vis[10005];
    priority_queue< node, vector<node> ,greater<node> > q;
    int main()
    {
        //freopen("in.txt","r",stdin);
        int tmp1,tmp2,tmp3;
        memset(a,-1,sizeof(0));
        memset(first,-1,sizeof(first));
        memset(vis,-1,sizeof(vis));
        memset(dis,0x3f,sizeof(dis));
        int t,n;
        scanf("%d%d",&t,&n);
        int cnt=1;
        for(int i=1;i<=t;i++)
        {
            scanf("%d%d%d",&tmp1,&tmp2,&tmp3);
            a[cnt].nex=first[tmp1];
            a[cnt].to=tmp2;
            a[cnt].w=tmp3;
            first[tmp1]=cnt;
            cnt++;
            a[cnt].nex=first[tmp2];
            a[cnt].to=tmp1;
            a[cnt].w=tmp3;
            first[tmp2]=cnt;
            cnt++;
        }
        dis[1]=0;
        node temp;
        temp.to=1;
        temp.w=0;
        temp.nex=0;
        q.push(temp);
        while(q.size())
        {
            //while q.sizeof()
            node temp2;
            temp=q.top();
            q.pop();
            //cout<<temp.to<<endl;
            if(vis[temp.to]==1) continue;
            vis[temp.to]=1;
            for(int i=first[temp.to];i!=-1;i=a[i].nex)
            {
                //cout<<a[i].to<<endl;
                if(dis[a[i].to]>dis[temp.to]+a[i].w)
                {
                    dis[a[i].to]=dis[temp.to]+a[i].w;
                    temp2.w=dis[a[i].to];
                    temp2.to=a[i].to;
                    q.push(temp2);
                }
            }
           // cout<<endl;
        }
        printf("%d
    ",dis[n]);
    }
    
  • 相关阅读:
    poj3225(区间操作,交,并,补)
    uva11235
    hdu1166(树状数组)
    uva11997
    uva11991
    uva 11995
    2017 Multi-University Training Contest
    Maven设置使用自定义的jar包到自己本地仓库
    Springboot之从数据库读取配置信息进行注入
    Springboot中为什么需要采用Service+ServiceImpl的结构?
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852320.html
Copyright © 2011-2022 走看看