zoukankan      html  css  js  c++  java
  • K最短路问题(单源点最短路径+A*算法)

    [cpp] view plain copy print ?
      1. /* 
      2.  *算法引入: 
      3.  *在单源点最短路径问题中,实际运用时还需知道最短路径外,次短路或者第三短路; 
      4.  *即要知道多条最短路,并排出其长度增加的顺序,即为K最短路问题; 
      5.  * 
      6.  *算法思想: 
      7.  *单源点最短路径+高级搜索A*; 
      8.  *A*算法结合了启发式方法和形式化方法; 
      9.  *启发式方法通过充分利用图给出的信息来动态地做出决定而使搜索次数大大降低; 
      10.  *形式化方法不利用图给出的信息,而仅通过数学的形式分析; 
      11.  * 
      12.  *算法通过一个估价函数f(h)来估计图中的当前点p到终点的距离,并由此决定它的搜索方向; 
      13.  *当这条路径失败时,它会尝试其他路径; 
      14.  *对于A*,估价函数=当前值+当前位置到终点的距离,即f(p)=g(p)+h(p),每次扩展估价函数值最小的一个; 
      15.  * 
      16.  *对于K短路算法来说,g(p)为当前从s到p所走的路径的长度;h(p)为点p到t的最短路的长度; 
      17.  *f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远; 
      18.  * 
      19.  *为了加速计算,h(p)需要在A*搜索之前进行预处理,只要将原图的所有边反向,再从终点t做一次单源点最短路径就能得到每个点的h(p)了; 
      20.  * 
      21.  *算法步骤: 
      22.  *(1),将有向图的所有边反向,以原终点t为源点,求解t到所有点的最短距离; 
      23.  *(2),新建一个优先队列,将源点s加入到队列中; 
      24.  *(3),从优先级队列中弹出f(p)最小的点p,如果点p就是t,则计算t出队的次数; 
      25.  *如果当前为t的第k次出队,则当前路径的长度就是s到t的第k短路的长度,算法结束; 
      26.  *否则遍历与p相连的所有的边,将扩展出的到p的邻接点信息加入到优先级队列; 
      27.  * 
      28.  *算法测试: 
      29.  *PKU2449(Remmarguts' Date) 
      30.  * 
      31.  *题目大意: 
      32.  *求从s到t的第k短路的长度; 
      33.  */  
      34.   
      35. #include<iostream>  
      36. #include<cstring>  
      37. #include<cstdlib>  
      38. #include<queue>  
      39. #include<cstdio>  
      40. #include<climits>  
      41. #include<algorithm>  
      42. using namespace std;  
      43.   
      44. const int INF=0xffffff;  
      45. const int N=1010;  
      46. const int M=100010;  
      47.   
      48. struct node1  
      49. {  
      50.     int to;  
      51.     int w;  
      52.     int next;  
      53. };  
      54.   
      55. node1 edge1[M],edge2[M];  
      56. int head1[M],head2[M];  
      57. int idx1,idx2;  
      58. int dist[N];  
      59.   
      60. struct node2  
      61. {  
      62.     int to;  
      63.     //g(p)为当前从s到p所走的路径的长度;h(p)为点p到t的最短路的长度;  
      64.     int g,f;//f=g+h,f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;  
      65.     bool operator<(const node2 &r ) const  
      66.     {  
      67.         if(r.f==f)  
      68.             return r.g<g;  
      69.         return r.f<f;  
      70.     }  
      71. };  
      72.   
      73. void Addedge1(int u,int v,int w)  
      74. {  
      75.     edge1[idx1].w=w;  
      76.     edge1[idx1].to=v;  
      77.     edge1[idx1].next=head1[u];  
      78.     head1[u]=idx1++;  
      79. }  
      80.   
      81. void Addedge2(int u,int v,int w)  
      82. {  
      83.     edge2[idx2].w=w;  
      84.     edge2[idx2].to=v;  
      85.     edge2[idx2].next=head2[u];  
      86.     head2[u]=idx2++;  
      87. }  
      88.   
      89. bool SPFA(int s,int n,int head[],node1 edge[],int dist[])  
      90. {  
      91.     queue<int>Q1;  
      92.     int inq[N];  
      93.     for(int i=0; i<=n; i++)  
      94.     {  
      95.         dist[i]=INF;  
      96.         inq[i]=0;  
      97.     }  
      98.     dist[s]=0;  
      99.     Q1.push(s);  
      100.     inq[s]++;  
      101.     while(!Q1.empty())  
      102.     {  
      103.         int q=Q1.front();  
      104.         Q1.pop();  
      105.         inq[q]--;  
      106.         if(inq[q]>n)//负权环  
      107.             return false;  
      108.         int k=head[q];  
      109.         while(k>=0)  
      110.         {  
      111.             if(dist[edge[k].to]>dist[q]+edge[k].w)  
      112.             {  
      113.                 dist[edge[k].to]=edge[k].w+dist[q];  
      114.                 if(!inq[edge[k].to])  
      115.                 {  
      116.                     inq[edge[k].to]++;  
      117.                     Q1.push(edge[k].to);  
      118.                 }  
      119.             }  
      120.             k=edge[k].next;  
      121.         }  
      122.     }  
      123.     return true;  
      124. }  
      125.   
      126. int A_star(int s,int t,int n,int k,int head[],node1 edge[],int dist[])  
      127. {  
      128.     node2 e,ne;  
      129.     int cnt=0;  
      130.     priority_queue<node2>Q;  
      131.     if(s==t)//当s==t时,距离为0的路不能算在这k短路中,所以需要求k+1短路;  
      132.         k++;  
      133.     if(dist[s]==INF)  
      134.         return -1;  
      135.     e.to=s;  
      136.     e.g=0;  
      137.     e.f=e.g+dist[e.to];  
      138.     Q.push(e);  
      139.   
      140.     while(!Q.empty())  
      141.     {  
      142.         e=Q.top();  
      143.         Q.pop();  
      144.         if(e.to==t)//找到一条最短路径  
      145.         {  
      146.             cnt++;  
      147.         }  
      148.         if(cnt==k)//找到k短路  
      149.         {  
      150.             return e.g;  
      151.         }  
      152.         for(int i=head[e.to]; i!=-1; i=edge[i].next)  
      153.         {  
      154.             ne.to=edge[i].to;  
      155.             ne.g=e.g+edge[i].w;  
      156.             ne.f=ne.g+dist[ne.to];  
      157.             Q.push(ne);  
      158.         }  
      159.     }  
      160.     return -1;  
      161. }  
      162.   
      163. int main()  
      164. {  
      165.     int n,m;  
      166.     //freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin);  
      167.     while(~scanf("%d%d",&n,&m))  
      168.     {  
      169.         memset(head1, -1, sizeof(head1));  
      170.         memset(head2, -1, sizeof(head2));  
      171.         idx1=idx2=0;  
      172.         int u,v,w;  
      173.         for(int i=0; i<m; i++)  
      174.         {  
      175.             scanf("%d%d%d",&u,&v,&w);  
      176.             Addedge1(u,v,w);  
      177.             Addedge2(v,u,w);  
      178.         }  
      179.         int s,t,k;  
      180.         scanf("%d%d%d",&s,&t,&k);  
      181.         SPFA(t,n,head2,edge2,dist);//以原终点t为源点,求反向图中t到所有点的最短距离;  
      182.         int res=A_star(s,t,n,k,head1,edge1,dist);  
      183.         printf("%d\n",res);  
      184.     }  
      185.     return 0;  

  • 相关阅读:
    C#使用结构体,输入5个人的学号,姓名,分数,按照成绩高低排列打印出来
    数据库考试试题
    数据库存储系统应用,超市小票系统
    数据库变量与语句
    练习:C#---类(身份证号截取生日、验证邮箱、DateTime)
    Chapter 3. 类---String类、Math类、Datetime类
    练习:C#---for循环(整数和、阶乘、楼梯)
    Chapter 2 C#语句---异常语句
    Chapter 2. C#语句---循环语句
    Chapter 2 C#语句---选择语句
  • 原文地址:https://www.cnblogs.com/fengyv/p/3053430.html
Copyright © 2011-2022 走看看