zoukankan      html  css  js  c++  java
  • 洛谷单元最短路标准版——spfa优化

    #include<cstdio>

    #include<queue>

    #include<vector>

    #include<cstring>

    using namespace std;

    int n,m,k;

    int dis[100001];

    vector<int>a[100001],zhi[100001];

    bool fin[100001];

    struct pp

    {

             int dian;

    }dian;

    priority_queue <pp>q;

    bool operator <(pp a,pp b)

    {

             return dis[a.dian]>dis[b.dian];

    }

    void shengcheng()

    {

             for(int i=1;i<=n;i++)

             {

                       dis[i]=1000000009;

             }

             dis[k]=0;

             fin[k]=1;

             while(q.empty()==0)

             {

                       int u=q.top().dian;q.pop();

                       fin[u]=0;

                       for(int i=0;i<a[u].size();i++)

                       {

                                int w=a[u][i];

                                if(dis[w]>dis[u]+zhi[u][i])

                                {

                                         dis[w]=dis[u]+zhi[u][i];

                                         if(fin[w]==0)

                                         {

                                                   fin[w]=1;

                                                   dian.dian=w;

                                                   q.push(dian);

                                         }

                                }

                       }

             }

    }

    int x,y,z;

    int main()

    {

             scanf("%d%d%d",&n,&m,&k);

             for(int i=1;i<=m;i++)

             {

                      

                       scanf("%d%d%d",&x,&y,&z);

                       a[x].push_back(y);

                       zhi[x].push_back(z);

                      

             }

             dian.dian=k;

             q.push(dian);

             shengcheng();

             for(int i=1;i<=n;i++)

             {

                       if(i!=k)

                       printf("%d ",dis[i]);

                       else

                       printf("0 ");

             }

             return 0;

    }

    我不会迪杰斯特拉,于是想出了这个来偷懒

  • 相关阅读:
    K2 的Workspace 遭遇400 RequestLength 错误修复
    从APM角度上看:NoSQL和关系数据库并无不同
    Mono的Google Native Client(NaCl)技术支持
    FastReport.Mono 一款为Mono Framework设计的功能全面的报表生成工具
    修改 Windows Host 文件工具
    采用Mono进行移动开发图书推荐
    MonoDevelop添加NuGet支持
    WCF Service Hosting的线程关联性Mono实现比.NET统一?
    [转]WiX v3.7——支持MSBuild、自更新及引用计数
    [转]度量驱动开发
  • 原文地址:https://www.cnblogs.com/fanhao050109/p/11455948.html
Copyright © 2011-2022 走看看