单源点最短路径+高级搜索A*

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