• <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解題報告

            這道題我用了Kruscal+并查集算的
            之前并查集用的不對,所以一直WA
            并查集代碼來自我的那本寫數據結構與算法分析c++版 knuth的徒孫的網站

            #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 閱讀(452) 評論(0)  編輯 收藏 引用 所屬分類: C++Practise

            久久久久国产| 欧美亚洲日本久久精品| 久久Av无码精品人妻系列| 色88久久久久高潮综合影院| 亚洲av日韩精品久久久久久a| 久久久久人妻一区二区三区vr| 国产一级做a爰片久久毛片| 国产女人aaa级久久久级| 欧美亚洲国产精品久久高清| 亚洲AV无码久久精品成人 | 国产成人香蕉久久久久| 久久综合久久鬼色| 久久久久亚洲AV成人片| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久福利资源国产精品999| 久久久久综合网久久| 久久久久久久综合日本| 97久久久久人妻精品专区| 国产免费久久精品99re丫y| 久久香蕉国产线看观看乱码| 无码八A片人妻少妇久久| 国产农村妇女毛片精品久久| 亚洲AV成人无码久久精品老人| 久久九九久精品国产| 国产精品久久精品| 色诱久久久久综合网ywww | 久久精品国产亚洲av高清漫画 | 久久线看观看精品香蕉国产| 亚洲AV无码久久精品蜜桃| 久久这里都是精品| 日本加勒比久久精品| 久久天天躁狠狠躁夜夜不卡| 国产一区二区精品久久| 久久99亚洲网美利坚合众国| 亚洲精品无码久久一线| 免费无码国产欧美久久18| 无码人妻久久一区二区三区蜜桃| 三级韩国一区久久二区综合| 久久天天日天天操综合伊人av| 久久国产热这里只有精品| 国产精品亚洲综合专区片高清久久久 |