• <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>

            我住包子山

            this->blog.MoveTo("blog.baozishan.in")

            zju1942解題報(bào)告

            這道題我用了Kruscal+并查集算的
            之前并查集用的不對(duì),所以一直WA
            并查集代碼來自我的那本寫數(shù)據(jù)結(jié)構(gòu)與算法分析c++版 knuth的徒孫的網(wǎng)站

            #include <cstdio>
            #include 
            <vector>
            #include 
            <cmath>
            #include 
            <algorithm>
            using namespace std;

            #ifndef DISJ_SETS_H
            #define DISJ_SETS_H

            // DisjSets class
            //
            // CONSTRUCTION: with int representing initial number of sets
            //
            // ******************PUBLIC OPERATIONS*********************
            // void union( root1, root2 ) --> Merge two sets
            // int find( x )              --> Return set containing x
            // ******************ERRORS********************************
            // No error checking is performed

            #include 
            <vector>
            using namespace std;

            /**
             * Disjoint set class.
             * Use union by rank and path compression.
             * Elements in the set are numbered starting at 0.
             
            */

            class DisjSets
            {
              
            public:
                
            explicit DisjSets( int numElements );

                
            int find( int x ) const;
                
            int find( int x );
                
            void unionSets( int root1, int root2 );

              
            private:
                vector
            <int> s;
            }
            ;

            #endif


            /**
             * Construct the disjoint sets object.
             * numElements is the initial number of disjoint sets.
             
            */

            DisjSets::DisjSets( 
            int numElements ) : s( numElements )
            {
                
            forint i = 0; i < s.size( ); i++ )
                    s[ i ] 
            = -1;
            }


            /**
             * Union two disjoint sets.
             * For simplicity, we assume root1 and root2 are distinct
             * and represent set names.
             * root1 is the root of set 1.
             * root2 is the root of set 2.
             
            */

            void DisjSets::unionSets( int root1, int root2 )
            {
            //    if( s[ root2 ] < s[ root1 ] )  // root2 is deeper
                    s[ root1 ] = root2;        // Make root2 new root
                
            //else
                
            //{
                
            //    if( s[ root1 ] == s[ root2 ] )
                
            //        s[ root1 ]--;          // Update height if same
                
            //    s[ root2 ] = root1;        // Make root1 new root
                
            //}
            }



            /**
             * Perform a find.
             * Error checks omitted again for simplicity.
             * Return the set containing x.
             
            */

            int DisjSets::find( int x ) const
            {
                
            if( s[ x ] < 0 )
                    
            return x;
                
            else
                    
            return find( s[ x ] );
            }



            /**
             * Perform a find with path compression.
             * Error checks omitted again for simplicity.
             * Return the set containing x.
             
            */

            int DisjSets::find( int x )
            {
                
            if( s[ x ] < 0 )
                    
            return x;
                
            else
                    
            return s[ x ] = find( s[ x ] );
            }



            struct Gedge
            {
                
            int startnumber;
                
            int endnumber;
                
            double weight;
                Gedge(
            int s,int e,double w):startnumber(s),endnumber(e),weight(w){}
            }
            ;
            struct Gnode
            {
                
            int x;
                
            int y;
                Gnode(
            int xx,int yy):x(xx),y(yy){}
                Gnode():x(
            0),y(0){}
                
            static double dis(const Gnode & n1, const Gnode & n2)
                
            {
                    
            double d1=(n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y);
                    
            return d1;
                }


            }
            ;

            bool comp(const Gedge& g1,const Gedge& g2)
            {
                
            return g1.weight<g2.weight;
            }

            vector
            <Gedge> edgeV;
            Gnode nodeArray[
            201];
            int main()
            {
                
            int n=0,cnt=1;
                
            while(scanf("%d",&n)!=EOF)
                
            {
                    
            if(n==0)break;
                    
            for(int i=0;i<n;i++)
                    
            {
                        scanf(
            "%d %d",&nodeArray[i].x,&nodeArray[i].y);
                    }

                    
            for(int i=0;i<n-1;i++)
                    
            {
                        
            for(int j=i+1;j<n;j++)
                        
            {
                            edgeV.push_back(Gedge(i,j,Gnode::dis(nodeArray[i],nodeArray[j])));
                        }

                    }

                    DisjSets ds(n
            +1);
                    
            double minstep=0;
                    sort(edgeV.begin(),edgeV.end(),comp);
                    
            while(ds.find(0)!=ds.find(1))
                    
            {
                        minstep
            =edgeV[0].weight;
                        
            int a=ds.find(edgeV[0].startnumber);
                        
            int b=ds.find(edgeV[0].endnumber);
                        
            if(a!=b)
                        
            {
                            ds.unionSets(a,b);               
                        }

                        edgeV.erase(edgeV.begin());
                    }

                    printf(
            "Scenario #%d\nFrog Distance = %.3lf\n\n",cnt++,sqrt(minstep));
                    edgeV.clear();
                }

            }

            posted on 2007-07-28 21:04 Gohan 閱讀(459) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++Practise

            97久久超碰国产精品2021| 亚洲?V乱码久久精品蜜桃| 少妇高潮惨叫久久久久久| 热re99久久精品国99热| 91久久精品国产成人久久| 人妻少妇精品久久| 麻豆一区二区99久久久久| 久久国产精品一区| 亚洲国产欧美国产综合久久 | 久久久SS麻豆欧美国产日韩| 久久久无码精品亚洲日韩按摩 | 中文字幕一区二区三区久久网站| 少妇被又大又粗又爽毛片久久黑人| 久久精品国产99国产精品亚洲| 久久久精品免费国产四虎| 2021国内精品久久久久久影院| 久久精品一区二区国产| 亚洲日本va中文字幕久久| 精品99久久aaa一级毛片| 婷婷伊人久久大香线蕉AV| 久久成人18免费网站| 久久电影网一区| 国内精品久久久久影院优| 麻豆久久久9性大片| 久久精品亚洲乱码伦伦中文| 久久久久久午夜成人影院| 精品久久久久久久久免费影院| 国产精自产拍久久久久久蜜| 91久久精品91久久性色| 久久天天躁狠狠躁夜夜网站| 99久久无色码中文字幕人妻| 亚洲国产成人久久综合一区77| 婷婷综合久久狠狠色99h| 精品久久一区二区三区| 精品综合久久久久久888蜜芽| 亚洲中文字幕无码久久精品1 | 久久精品人人做人人妻人人玩| 久久婷婷五月综合色奶水99啪| 人妻中文久久久久| 久久综合久久伊人| 香蕉久久夜色精品国产尤物|