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]);
    }
    
  • 相关阅读:
    计算机网络知识
    数据库知识
    操作系统知识
    计算机硬件基础知识
    计算机科学基础知识
    2019下半年软件设计师考试大纲
    软件设计师补题(2008下半年上午题)
    软件设计师补题(2008上半年上午题)
    测试复盘3
    测试复盘2
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852320.html
Copyright © 2011-2022 走看看