• <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
            源地址倒是忘了,搜搜吧,這個應該容易理解的  回復  更多評論
              

            亚洲欧洲精品成人久久奇米网| 久久天天躁狠狠躁夜夜avapp| 成人综合伊人五月婷久久| 久久久久久精品免费免费自慰| 国产精品美女久久福利网站| 久久发布国产伦子伦精品| 日本道色综合久久影院| 久久精品桃花综合| 国产精品久久久久国产A级| 91麻豆精品国产91久久久久久| 久久精品国产清自在天天线| 中文字幕无码精品亚洲资源网久久| 久久99国内精品自在现线| 99久久婷婷国产综合精品草原| 午夜精品久久久久9999高清| 久久r热这里有精品视频| 久久人人爽人人爽人人片av麻烦 | 久久精品国产亚洲αv忘忧草| 亚洲乱码精品久久久久..| 狠狠色伊人久久精品综合网| 亚洲国产精品无码久久久秋霞2 | 午夜视频久久久久一区| 伊人久久大香线蕉综合影院首页| 免费观看成人久久网免费观看| 一本色道久久88精品综合| 久久久综合香蕉尹人综合网| 久久狠狠高潮亚洲精品| 午夜精品久久久内射近拍高清 | 久久综合亚洲色一区二区三区| 久久99国产精品久久99| 日韩久久久久久中文人妻| 久久久久久久女国产乱让韩| 国产午夜电影久久| 7国产欧美日韩综合天堂中文久久久久| 漂亮人妻被中出中文字幕久久 | 婷婷久久久亚洲欧洲日产国码AV| 久久精品夜色噜噜亚洲A∨| 四虎国产永久免费久久| 99精品久久久久中文字幕| 国产产无码乱码精品久久鸭| 久久久精品人妻一区二区三区蜜桃|