• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            T9的空間

            You will never walk alone!

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              69 隨筆 :: 0 文章 :: 28 評論 :: 0 Trackbacks

            網上看到的spfa的實現,很清晰。Orz~~

            #include <cstdio>
            #include 
            <cstring>
            const int maxn = 10000+1;
            const int maxnm = 100000+1;
            class node 
            {
                  
            public:
                         
            int l,to;
                         node 
            * next;    
            }
            ;
            template
            <class T>
            class queue{
            private:
                
            long N;
                T
            * s;
                
            long beg,end;
            public:
                inline 
            void init(){
                 beg
            =end=0;
                }

                queue(
            long size):N(size),s(new T[size]){
                 init();
                }

                inline 
            void push(const T& item){
                 s[end]
            =item;
                 
            ++end;
                 end
            %=N;
                }

                inline T pop()
            {
                 T res
            =s[beg];
                 
            ++beg;
                 beg
            %=N;
                 
            return res;
                }

                inline 
            bool empty(){
                 
            return beg==end;
                }

            }
            ;

            node 
            *f[maxnm];
            int n,m,s,t,ans;
            int dist[maxn];
            bool p[maxn];//判斷點是否在隊列當中
            inline void init()
            {
                    FILE 
            *fin = fopen("sssp.in","r");
                    
            int i,j,p1,p2,len;
                    node 
            *tp;
                    fscanf(fin,
            "%d%d",&n,&m);
                    
            for (i = 1 ; i <= m ; i++)
                    
            {
                           fscanf(fin,
            "%d%d%d",&p1,&p2,&len);
                           tp 
            = new node;
                           tp 
            -> l = len;
                           tp 
            -> to = p2; 
                           tp 
            -> next = f[p1];
                           f[p1] 
            = tp;                  
                    }
                
                    fscanf(fin,
            "%d%d",&s,&t);   
            }


            inline 
            void work()
            {
                     
            int i,j,k;
                     node 
            * tp;
                     queue 
            <int> que(n+1);
                     que.init();
                     memset(dist,
            126,sizeof(dist));
                     dist[s] 
            = 0;
                     que.push(s);
                     p[s] 
            = true;
                     
            do 
                     

                          i 
            = que.pop();
                          tp 
            = f[i];
                          p[i] 
            =    false;    //p[i] = false 表示i點不在隊列當中
                          while (tp!=NULL)
                          
            {
                             
            if (dist[i]+tp->l<dist[tp->to])     
                                
            {
                                    dist[tp
            ->to] = dist[i]+tp->l;
                                    
            if (!p[tp->to])     //如果tp->to沒有在隊列當中,那就將它加進去
                                    {
                                       que.push(tp
            ->to);
                                       p[tp
            ->to] = true;
                                    }
             
                                }
                  
                                tp 
            = tp->next;
                          }
                     
                     }
            while (!que.empty());
                   
            }

            inline 
            void print()
            {
                    FILE 
            *fout = fopen("sssp.out","w");
                    fprintf(fout,
            "%d\n",dist[t]);       
            }

            int main()
            {
                    init();
                    work();
                    print();
                    
            return 0;    
            }



            還有一種實現方法用了STL的queue,構圖的方法很好是一個一維的加一個p數組
            #include <iostream>
            #include 
            <queue>
            using namespace std;

            const long MAXN=10000;
            const long lmax=0x7FFFFFFF;

            typedef 
            struct  
            {
                
            long v;
                
            long next;
                
            long cost;
            }
            Edge;


            Edge e[MAXN];
            long p[MAXN];
            long Dis[MAXN];
            bool vist[MAXN];

            queue
            <long> q;

            long m,n;//點,邊
            void init()
            {
                
            long i;
                
            long eid=0;

                memset(vist,
            0,sizeof(vist));
                memset(p,
            -1,sizeof(p));
                fill(Dis,Dis
            +MAXN,lmax);

                
            while (!q.empty())
                
            {
                    q.pop();
                }


                
            for (i=0;i<n;++i)
                
            {
                    
            long from,to,cost;
                    scanf(
            "%ld %ld %ld",&from,&to,&cost);

                    e[eid].next
            =p[from];
                    e[eid].v
            =to;
                    e[eid].cost
            =cost;
                    p[from]
            =eid++;

                    
            //以下適用于無向圖
                    swap(from,to);
                    
                    e[eid].next
            =p[from];
                    e[eid].v
            =to;
                    e[eid].cost
            =cost;
                    p[from]
            =eid++;

                }

            }


            void print(long End)
            {
                
            //若為lmax 則不可達
                printf("%ld\n",Dis[End]);    
            }


            void SPF()
            {

                init();

                
            long Start,End;
                scanf(
            "%ld %ld",&Start,&End);
                Dis[Start]
            =0;
                vist[Start]
            =true;
                q.push(Start);

                
            while (!q.empty())
                
            {
                    
            long t=q.front();
                    q.pop();
                    vist[t]
            =false;
                    
            long j;
                    
            for (j=p[t];j!=-1;j=e[j].next)
                    
            {
                        
            long w=e[j].cost;
                        
            if (w+Dis[t]<Dis[e[j].v])
                        
            {
                            Dis[e[j].v]
            =w+Dis[t];
                            
            if (!vist[e[j].v])
                            
            {
                                vist[e[j].v]
            =true;
                                q.push(e[j].v);
                            }

                        }

                    }

                }


                print(End);

            }


            int main()
            {
                
            while (scanf("%ld %ld",&m,&n)!=EOF)
                
            {
                    SPF();
                }

                
            return 0;
            }
            posted on 2008-09-11 17:01 Torres 閱讀(1087) 評論(2)  編輯 收藏 引用 所屬分類: Graph

            評論

            # re: SPFA (Shortest Path Faster Algorithm) 2009-03-26 19:29 xxx
            靜態與動態區別,能不能把原地址貼出來  回復  更多評論
              

            # re: SPFA (Shortest Path Faster Algorithm) 2009-03-26 22:35 Torres
            源地址倒是忘了,搜搜吧,這個應該容易理解的  回復  更多評論
              

            久久免费视频1| 久久综合九色综合久99| 久久福利资源国产精品999| 青青草原综合久久大伊人导航| 久久久亚洲欧洲日产国码是AV| 久久夜色精品国产噜噜噜亚洲AV | 国产精品免费看久久久香蕉| 美女久久久久久| 久久狠狠高潮亚洲精品 | 亚洲欧洲中文日韩久久AV乱码| 国产精品99久久久精品无码| 91精品国产综合久久香蕉 | 伊人久久精品无码av一区| 亚洲精品高清久久| 久久夜色精品国产噜噜麻豆| 久久久WWW免费人成精品| 国产精品久久久亚洲| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久国语露脸国产精品电影 | 久久久噜噜噜久久| 精品久久久久久久无码 | 久久偷看各类wc女厕嘘嘘| 久久精品中文字幕有码| 狠狠色丁香婷婷久久综合不卡| AAA级久久久精品无码区| 久久综合一区二区无码| 青青草原综合久久大伊人精品| 伊人久久亚洲综合影院| 久久线看观看精品香蕉国产| 国产成人久久精品一区二区三区| 性做久久久久久久久浪潮| 久久国产免费直播| 国产91久久综合| 国产香蕉97碰碰久久人人| 国产免费久久精品99久久| 久久综合综合久久狠狠狠97色88| 久久婷婷成人综合色综合| 国产成人精品久久| 久久久亚洲欧洲日产国码二区| 亚洲国产精品久久久天堂| 一本一本久久A久久综合精品|