• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              69 隨筆 :: 0 文章 :: 28 評(píng)論 :: 0 Trackbacks

            網(wǎng)上看到的spfa的實(shí)現(xiàn),很清晰。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];//判斷點(diǎn)是否在隊(duì)列當(dāng)中
            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點(diǎn)不在隊(duì)列當(dāng)中
                          while (tp!=NULL)
                          
            {
                             
            if (dist[i]+tp->l<dist[tp->to])     
                                
            {
                                    dist[tp
            ->to] = dist[i]+tp->l;
                                    
            if (!p[tp->to])     //如果tp->to沒有在隊(duì)列當(dāng)中,那就將它加進(jìn)去
                                    {
                                       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;    
            }



            還有一種實(shí)現(xiàn)方法用了STL的queue,構(gòu)圖的方法很好是一個(gè)一維的加一個(gè)p數(shù)組
            #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;//點(diǎ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++;

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

                }

            }


            void print(long End)
            {
                
            //若為lmax 則不可達(dá)
                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 閱讀(1091) 評(píng)論(2)  編輯 收藏 引用 所屬分類: Graph

            評(píng)論

            # re: SPFA (Shortest Path Faster Algorithm) 2009-03-26 19:29 xxx
            靜態(tài)與動(dòng)態(tài)區(qū)別,能不能把原地址貼出來(lái)  回復(fù)  更多評(píng)論
              

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

            久久久久人妻一区精品色| 国产精品欧美亚洲韩国日本久久| 国内精品伊人久久久久av一坑| 97精品国产97久久久久久免费| 久久频这里精品99香蕉久| 一级女性全黄久久生活片免费 | 五月丁香综合激情六月久久 | 麻豆久久久9性大片| 亚洲人AV永久一区二区三区久久| 亚洲精品综合久久| 精品综合久久久久久97超人 | 久久亚洲AV无码西西人体| 国产免费久久精品99re丫y| 影音先锋女人AV鲁色资源网久久 | 蜜桃麻豆WWW久久囤产精品| 久久久无码人妻精品无码| 国产成人综合久久精品尤物| 青青草原综合久久大伊人精品| 久久99精品久久久久久水蜜桃 | 久久精品成人免费网站| 久久精品国产国产精品四凭| 91久久精品电影| 99久久精品国内| 亚洲精品高清国产一线久久 | 香蕉久久永久视频| 精品人妻伦一二三区久久| 国色天香久久久久久久小说| 久久精品中文闷骚内射| 久久久久国产一区二区 | 国产毛片欧美毛片久久久| 久久精品九九亚洲精品天堂 | 亚洲综合婷婷久久| 国产成人综合久久综合| 狠狠色婷婷久久综合频道日韩 | 日本道色综合久久影院| 久久久久国产精品嫩草影院| 国内精品久久久久影院一蜜桃| 亚洲精品无码久久毛片| 欧美色综合久久久久久| 久久99精品国产麻豆不卡| 88久久精品无码一区二区毛片|