• HDU 1874(简单最短路) (大优化)


    优先队列那里用greater会报错

    http://acm.hdu.edu.cn/showproblem.php?pid=1874

     1 /*
     2 使用pair代替结构
     3 */
     4 
     5 #include <iostream>
     6 #include <cstdio>
     7 #include <queue>
     8 #include <vector>
     9 using namespace std;
    10 const int Ni = 10000;
    11 const int INF = 1<<27;
    12 
    13 typedef pair<int,int> pa;
    14 
    15 int dis[Ni],n;//dis使用1-n的部分
    16 
    17 vector<pair<int,int> > eg[Ni];
    18 
    19 void Dijkstra(int s)
    20 {
    21           int i,j;
    22           for(i=0;i<=n;i++)//要到n
    23                     dis[i] = INF;
    24           priority_queue<pa> q;  //优先级队列:小顶堆
    25           dis[s] = 0;
    26           q.push(make_pair(s,dis[s]));
    27 
    28           while(!q.empty())
    29           {
    30                     pa x = q.top();
    31                     q.pop();
    32                     int w = x.first;
    33                     for(j = 0;j<eg[w].size();j++)//遍历x的所有邻接点
    34                     {
    35                               pa y = eg[w][j];//y是x的邻接点
    36                               int u = y.first;
    37                               if(dis[u]>x.second+y.second)
    38                               {
    39                                         dis[u] = x.second+y.second;
    40                                         q.push(make_pair(u,dis[u]));
    41                               }
    42                     }
    43           }
    44 
    45 }
    46 
    47 
    48 int main()
    49 {
    50           int m,a,b,d;//关系个数
    51           while(cin>>n>>m)
    52           {
    53                     for(int i = 0;i<=n;i++)
    54                               eg[i].clear();//初始化
    55                     while(m--)
    56                     {
    57                               cin>>a>>b>>d;
    58                               eg[a].push_back(make_pair(b,d));
    59                               eg[b].push_back(make_pair(a,d));
    60                     }
    61 
    62                     int o,p;
    63                     cin>>o>>p;
    64                     Dijkstra(o);
    65                     if(dis[p]!=INF)
    66                               cout<<dis[p]<<endl;
    67                     else
    68                               cout<<"-1
    ";
    69           }
    70 
    71           return 0;
    72 }
    73 /*
    74 6 6
    75 1 2 2
    76 3 2 4
    77 1 4 5
    78 2 5 2
    79 3 6 3
    80 5 6 3
    81 */

    上面那种方法在ACM里实在太慢了(46MS),用C的话效率急速提升,达到了0MS。。

    #include <cstdio>
    using namespace std;
    const int L = 1010;
    const int INF = 1<<27;
    
    int map[L][L];
    int vis[L];
    int dis[L];
    
    int n,m;
    
    void Dijkstra(int s)
    {
              int i,j,k;
              int min;
              for(i = 0;i<n;i++)
              {
                        dis[i] = map[s][i];
                        vis[i] = 0;
              }
    
              vis[s] = 1;
              dis[s] = 0;
    
              for(i = 0;i<n;i++)
              {
                        min = INF;
                        for(j = 0;j<n;j++)
                        {
                                  if(dis[j]<min && !vis[j])
                                  {
                                            k = j;
                                            min = dis[j];
                                  }
                        }
    
                        vis[k] = 1;
    
                        for(j = 0;j<n;j++)
                        {
                                  if(dis[j] > min+map[k][j] && !vis[j])
                                  {
                                            dis[j] = min+map[k][j];
                                  }
                        }
              }
    }
    
    void init()
    {
              int i,j;
              for(i = 0;i<n;i++)
              {
                        map[i][i] = 0;
                        for(j = i+1;j<n;j++)
                        {
                                  map[i][j] = map[j][i] = INF;
                        }
              }
    
              while(m--)
              {
                        int a,b,w;
                        scanf("%d%d%d",&a,&b,&w);
                        if(w<map[a][b])//可能有同两点,但不同weight
                        {
                                  map[a][b] = w;
                                  map[b][a] = w;
                        }
              }
    }
    
    
    int main()
    {
              while(~scanf("%d%d",&n,&m))
              {
                        int r,t;
    
                        init();
    
                        scanf("%d%d",&r,&t);
    
                        Dijkstra(r);
    
                        if(dis[t]!=INF)
                                  printf("%d
    ",dis[t]);
                        else
                                  printf("-1
    ");
    
              }
    
              return 0;
    }
  • 相关阅读:
    python 3.5.2安装mysql驱动报错
    mysql创建用户
    Centos7 修改SSH 端口
    centos 7 升级后yum install出现Exiting on user cancel
    centos7 systemctl 启动 Redis 失败
    redis 异常解决办法
    用普通用户通过sudo进行启动tomcat时报如下异常
    svn迁移到git仓库并保留commit历史记录
    CentOS更新yum源
    django 添加comments app
  • 原文地址:https://www.cnblogs.com/qlky/p/5015740.html
Copyright © 2020-2023  润新知