zoukankan      html  css  js  c++  java
  • POJ2449 A*+第K短路

    题意:s到t的第K短路

    View Code
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<math.h>
      4 #include<stdlib.h>
      5 #include<algorithm>
      6 #include<queue>
      7 using namespace std;
      8 const int maxn = 1005;
      9 const int maxm = 200005;
     10 const int inf = 9999999;
     11 struct node{
     12     int u,val,next;
     13 }edge[ maxm ];
     14 struct node2{
     15     int u,h,d;
     16     bool operator <( node2 a )const 
     17     {
     18         return a.d+a.h<d+h;
     19     }
     20 };
     21 int cnt,head[ maxn ],tail[ maxn ];
     22 int n;
     23 int h[ maxn ],vis[ maxn ];
     24 
     25 void init(){
     26     cnt=0;
     27     memset( head,-1,sizeof( head ));
     28     memset( tail,-1,sizeof( tail ));
     29 }
     30 /*
     31 void addedge( int u,int v,int c,int e )  
     32 {  
     33      edge[e<<1].u=v;  
     34      edge[e<<1].val=c;  
     35      edge[e<<1].next=head[u];  
     36      head[u]=e<<1;  
     37        
     38      edge[e<<1|1].u=u;  
     39      edge[e<<1|1].val=c;  
     40      edge[e<<1|1].next=tail[v];  
     41      tail[v]=e<<1|1;  
     42      return ;  
     43 }  
     44 */
     45 
     46 void addedge( int a,int b,int c ){
     47     edge[ cnt ].u=b;
     48     edge[ cnt ].val=c;
     49     edge[ cnt ].next=head[ a ];
     50     head[ a ]=cnt++;
     51 
     52     edge[ cnt ].u=a;
     53     edge[ cnt ].val=c;
     54     edge[ cnt ].next=tail[b];
     55     tail[ b ]=cnt++;
     56 
     57 }
     58 
     59 
     60 void Dij( int s,int t ){
     61     for( int i=1;i<=n;i++ ){
     62         h[ i ]=inf;
     63         vis[ i ]=0;
     64     }
     65     h[ s ]=0;
     66     for( int i=1;i<=n;i++ ){
     67         int min,k;
     68         min=inf;
     69         for( int j=1;j<=n;j++ ){
     70             if( min>h[ j ]&&vis[j]==0 ){
     71                 min=h[j];
     72                 k=j;
     73             }
     74         }
     75         if( min==inf ) break;
     76         vis[ k ]=1;
     77         for( int j=tail[ k ];j!=-1;j=edge[ j ].next ){
     78             int v=edge[ j ].u;
     79             if( vis[ v ]==0 && h[ v ]>h[ k ]+edge[ j ].val ){
     80                 h[ v ]=h[ k ]+edge[ j ].val;
     81             }
     82         }
     83     }
     84     return ;
     85 }
     86 
     87 int A_star( int s,int t,int k ){
     88     node2 cur,next;
     89     priority_queue<node2>q;
     90     while( !q.empty() )
     91         q.pop();
     92     int cnt[ maxn ];
     93     memset( cnt,0,sizeof( cnt ));
     94     cur.u=s;
     95     cur.h=h[ s ];
     96     cur.d=0;
     97     q.push( cur );
     98     while( !q.empty() ){
     99         cur=q.top();
    100         q.pop();
    101         cnt[ cur.u ]++;
    102         if( cnt[ cur.u ]>k ) continue;
    103         if( cnt[ t ]==k )return cur.d;
    104         for( int i=head[ cur.u ];i!=-1;i=edge[ i ].next ){
    105             next.u=edge[ i ].u;
    106             next.d=cur.d+edge[ i ].val;
    107             next.h=h[ next.u ];
    108             q.push( next );
    109         }
    110     }
    111     return -1;
    112 }
    113 
    114 int main(){
    115     int m;
    116     while( scanf("%d%d",&n,&m)!=EOF ){
    117         int a,b,c;
    118         init();
    119         for( int i=0;i<m;i++ ){
    120             scanf("%d%d%d",&a,&b,&c);
    121             addedge( a,b,c );
    122         }
    123         int s,t,k;
    124         scanf("%d%d%d",&s,&t,&k);
    125         if( s==t )k++;
    126         Dij( t,s );
    127         printf("%d\n",A_star( s,t,k ));
    128     }
    129     return 0;
    130 }
    keep moving...
  • 相关阅读:
    全卷积网络(FCN)与图像分割
    Mac下编译tesseract报错 DotProductAVX can't be used on Android
    OSTU二值化算法
    ssh的用户配置文件config管理ssh会话
    SSD: Single Shot MultiBox Detector 编译方法总结
    论文笔记——A Deep Neural Network Compression Pipeline: Pruning, Quantization, Huffman Encoding
    LeetCode——Word Break
    C#多线程编程
    impinj 编程接口
    C# 委托实例(跨窗体操作控件)
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2923764.html
Copyright © 2011-2022 走看看