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

            POJ 3164 Command Network 最小樹形圖

            Description

            After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project.

            With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.

            Input

            The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.

            Output

            For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy’.

            Sample Input

            4 6
            0 6
            4 6
            0 0
            7 20
            1 2
            1 3
            2 3
            3 4
            3 1
            3 2
            4 3
            0 0
            1 0
            0 1
            1 2
            1 3
            4 1
            2 3

            Sample Output

            31.19
            poor snoopy

            Source


             

            最小樹形圖算法(Zhu-Liu Algorithm)

            1.       設(shè)最小樹形圖的總權(quán)值為cost,置cost0

            2.       除源點(diǎn)外,為其他所有節(jié)點(diǎn)Vi找一條權(quán)值最小的入邊,加入集合TT就是最短邊的集合。加邊的方法:遍歷所有點(diǎn)到Vi的邊中權(quán)值最小的加入集合T,記pre[Vi]為該邊的起點(diǎn),mincost[Vi]為該邊的權(quán)值。

            3.       檢查集合T中的邊是否存在有向環(huán),有則轉(zhuǎn)到步驟4,無則轉(zhuǎn)到步驟5。這里需要利用pre數(shù)組,枚舉檢查過的點(diǎn)作為搜索的起點(diǎn),類似dfs的操作判斷有向環(huán)。

            4.       將有向環(huán)縮成一個(gè)點(diǎn)。設(shè)環(huán)中有點(diǎn){Vk1,Vk2,…,Vki}i個(gè)點(diǎn),用Vk代替縮成的點(diǎn)。在壓縮后的圖中,更新所有不在環(huán)中的點(diǎn)VVk的距離:

            map[V][Vk] = min {map[V][Vkj]-mincost[Vki]} 1<=j<=i

            map[Vk][V] = min {map[Vkj][V]}           1<=j<=I

            5.       cost加上T中有向邊的權(quán)值總和就是最小樹形圖的權(quán)值總和。

            #include <iostream>
            #include 
            <cmath>

            #define min(a,b) (a<b ? a:b)

            const int MAXN = 110;
            const int INF = 0x7FFFFFFF;
            int n,m,pre[MAXN];
            double x[MAXN],y[MAXN];
            bool circle[MAXN],visit[MAXN];
            double ans,map[MAXN][MAXN];

            inline 
            double distance(double x1,double y1,double x2,double y2){
                
            return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            }

            void dfs(int u){
                visit[u]
            =true;
                
            for(int i=2;i<=n;i++)
                    
            if(!visit[i] && map[u][i]!=INF)
                        dfs(i);
            }

            bool connected(){
                memset(visit,
            false,sizeof(visit));
                
            int i,cnt=0;
                
            for(i=1;i<=n;i++)
                    
            if(!visit[i])
                        dfs(i),cnt
            ++;
                
            return cnt==1 ? true : false;
            }

            void min_arborescence(){
                
            int i,j,k;
                memset(circle,
            false,sizeof(circle));
                
            while(true){
                    
            for(i=2;i<=n;i++){
                        
            if(circle[i]) continue;
                        pre[i]
            =i;
                        map[i][i]
            =INF;
                        
            for(j=1;j<=n;j++){
                            
            if(circle[j]) continue;
                            
            if(map[j][i]<map[pre[i]][i])
                                pre[i]
            =j;
                        }

                    }

                    
            for(i=2;i<=n;i++){
                        
            if(circle[i]) continue;
                        j
            =i;
                        memset(visit,
            false,sizeof(visit));
                        
            while(!visit[j] && j!=1){
                            visit[j]
            =true;
                            j
            =pre[j];
                        }

                        
            if(j==1continue;
                        i
            =j;
                        ans
            +=map[pre[i]][i];
                        
            for(j=pre[i];j!=i;j=pre[j]){
                            ans
            +=map[pre[j]][j];
                            circle[j]
            =true;
                        }

                        
            for(j=1;j<=n;j++){
                            
            if(circle[j]) continue;
                            
            if(map[j][i]!=INF)
                                map[j][i]
            -=map[pre[i]][i];
                        }

                        
            for(j=pre[i];j!=i;j=pre[j])
                            
            for(k=1;k<=n;k++){
                                
            if(circle[k]) continue;
                                map[i][k]
            =min(map[i][k],map[j][k]);
                                
            if(map[k][j]!=INF)
                                    map[k][i]
            =min(map[k][i],map[k][j]-map[pre[j]][j]);
                            }

                        
            break;
                    }

                    
            if(i>n){
                        
            for(j=2;j<=n;j++){
                            
            if(circle[j]) continue;
                            ans
            +=map[pre[j]][j];
                        }

                        
            break;
                    }

                }

            }

            int main(){
                
            int i,j,u,v;
                
            while(scanf("%d %d",&n,&m)!=EOF){
                    
            for(ans=i=0;i<=n;i++for(j=0;j<=n;j++) map[i][j]=INF;
                    
            for(i=1;i<=n;i++) scanf("%lf %lf",&x[i],&y[i]);
                    
            while(m--){
                        scanf(
            "%d %d",&u,&v);
                        map[u][v]
            =distance(x[u],y[u],x[v],y[v]);
                    }

                    
            if(!connected()) puts("poor snoopy");
                    
            else{
                        min_arborescence();
                        printf(
            "%.2lf\n",ans);
                    }

                }

                
            return 0;
            }

            posted on 2009-05-26 16:03 極限定律 閱讀(676) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年6月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            伊人久久久AV老熟妇色| 久久这里有精品视频| 久久人妻少妇嫩草AV蜜桃| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 无码任你躁久久久久久| 午夜精品久久久久9999高清| 色偷偷88欧美精品久久久| 久久99热这里只有精品66| 狠狠色噜噜色狠狠狠综合久久| 亚洲精品乱码久久久久久| AV狠狠色丁香婷婷综合久久| 精品久久久久一区二区三区| 亚洲欧美一区二区三区久久| 久久精品无码一区二区无码| 国产AV影片久久久久久| 99久久做夜夜爱天天做精品| 国产精品久久自在自线观看| 久久久久亚洲AV无码去区首| 午夜精品久久久久久久久| 国产激情久久久久影院| 午夜天堂精品久久久久| 久久久久久国产a免费观看不卡| 国内高清久久久久久| 91久久精品国产成人久久| 99久久精品国产一区二区 | 久久精品免费观看| 人妻无码αv中文字幕久久琪琪布| 色婷婷综合久久久久中文| 久久久久无码国产精品不卡| 久久精品国产亚洲av高清漫画| 欧美久久天天综合香蕉伊| 久久香蕉一级毛片| 九九精品99久久久香蕉| 久久久久亚洲AV无码观看| 久久国产乱子伦精品免费午夜| 精品久久久久久国产| 久久免费的精品国产V∧| 久久精品国产亚洲AV影院| 午夜精品久久久内射近拍高清| 国产99久久久国产精免费| 久久电影网2021|