• <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>
            posts - 33,  comments - 33,  trackbacks - 0
            題意:給出一個(gè)無向圖,求在已知頂點(diǎn)v0的度不超過K的情況下,所得的最小生成樹
            題解:
            首先不考慮v0點(diǎn),先求得v1-v(n-1)的MST,然后分兩種情況考慮:
            令d為v0的度
            情況1 : 當(dāng)d == 1,時(shí) ,答案顯然是min{edge(0,i)}+MST{v1-v(n-1)}
            當(dāng) 1 < d <= K時(shí),考慮逐步添加一條{0-i}邊,添加邊后勢(shì)必構(gòu)成回路,然后在回路中找到
            權(quán)值最大的邊,然后在MST中將這條邊刪除并修改為{0-i}
            代碼:
            #include <stdio.h>
            #include 
            <algorithm>
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <map>
            using namespace std;

            const int V = 21;
            const int INF = 1 << 30;
            int n;

            struct Edge
            {
                
            int from;
                
            int to;
                
            int weight;
            }
            ;

            map
            <string,int> Map;
            int graph[V][V];
            Edge edges[V];
            int vertexNum;
            int s;
            bool visited[V];//邊(0,i)是否在edges中

            void init()
            {
                memset(visited,
            0,sizeof(visited));
                Map.clear();
                
            for(int i = 0; i < V; ++i)
                
            {
                    
            for(int j = 0; j < V; ++j)
                    
            {
                        graph[i][j] 
            = INF;
                    }

                }

            }



            void input()
            {
                
            string name1,name2;
                
            int dis;
                Map[
            "Park"= 0;
                
            int k = 1;
                
            for(int i = 0; i < n; ++i)
                
            {
                    cin 
            >> name1 >> name2 >> dis;
                    
            if(Map.find(name1) == Map.end())
                        Map[name1] 
            = k++;
                    
            if(Map.find(name2) == Map.end())
                        Map[name2] 
            = k++;
                    
            int id1 = Map[name1];
                    
            int id2 = Map[name2];
                    graph[id1][id2] 
            = graph[id2][id1] = dis;
                }

                scanf(
            "%d",&s);
            }


            //求v0 - v(_vertexNum-1)的最小生成樹
            int Prim(int _vertexNum)
            {
                
            int mstWeight = 0;
                
            for(int i = 1; i < _vertexNum - 1++i)
                
            {
                    edges[i].from 
            = 1;
                    edges[i].to 
            = i + 1;
                    edges[i].weight 
            = graph[1][i+1];
                }

                
            for (int i = 2; i < _vertexNum; ++i)
                
            {
                    
            int id = i-1;
                    
            int minW = edges[i-1].weight;
                    
            for (int j = i; j < _vertexNum-1++j)
                    
            {
                        
            if (minW > edges[j].weight)
                        
            {
                            minW 
            = edges[j].weight;
                            id 
            = j;
                        }

                    }

                    mstWeight 
            += minW;

                    swap(edges[i
            -1],edges[id]); 
                    
            int k = edges[i-1].to;

                    
            for (int j = i; j < _vertexNum -1++j)
                    
            {
                        
            int v = edges[j].to;
                        
            int w = graph[k][v];
                        
            if(w < edges[j].weight)
                        
            {
                            edges[j].weight 
            = w;
                            edges[j].from 
            = k;
                        }

                    }

                }

                
            return mstWeight;
            }


            //返回回路中最大的邊
            bool isCycle;
            void maxWeightInCycle(int _mv,int _from,int _to,int& _maxW,int& _id)
            {
                
            if (_to == _mv)
                
            {
                    isCycle 
            = true;
                    
            return;
                }

                
            for (int i = 0; i < vertexNum-1++i)
                
            {
                    
            if (edges[i].from != _to && edges[i].to != _to)
                    
            {
                        
            continue;
                    }

                    
            if (edges[i].from == _to && edges[i].to != _from)
                    
            {
                        maxWeightInCycle(_mv,_to,edges[i].to,_maxW,_id);
                        
            if (isCycle)
                        
            {
                            
            if (_maxW < edges[i].weight && edges[i].to != 0)
                            
            {
                                _maxW 
            = edges[i].weight;
                                _id 
            = i;
                            }

                            
            break;
                        }

                    }

                    
            else if(edges[i].to == _to && edges[i].from != _from)
                    
            {
                        maxWeightInCycle(_mv,_to,edges[i].from,_maxW,_id);
                        
            if (isCycle)
                        
            {
                            
            if (_maxW < edges[i].weight && edges[i].from != 0)
                            
            {
                                _maxW 
            = edges[i].weight;
                                _id 
            = i;
                            }

                            
            break;
                        }

                    }

                }

            }


            void Test()
            {
                init();
                input();
                vertexNum 
            = Map.size();
                
                
            int ans = Prim(vertexNum);//v0 - vn-1
                int minW = INF; 
                
            int id = -1;
                
            for (int i = 1; i < vertexNum; ++i)
                
            {
                    
            if (minW > graph[0][i])
                    
            {
                        minW 
            = graph[0][i];
                        id 
            = i;
                    }

                }

                ans 
            += graph[0][id];
                visited[id] 
            = true;
                
            //添加邊(0,id)
                edges[0].from = 0;
                edges[
            0].to = id;
                edges[
            0].weight = minW;

                
            //枚舉頂點(diǎn)0 的度
                for (int d = 2; d <= s; ++d)
                
            {
                    
            int dec = INF;
                    
            int edgeId = -1;
                    id 
            = -1;
                    
            for (int i = 1; i < vertexNum; ++i)
                    
            {
                        
            if (visited[i])//已經(jīng)在MST樹中了
                        {
                            
            continue;
                        }

                        
            int maxW = 0,maxId = -1;
                        isCycle 
            = false;
                        
            //添加邊(0-i),并返回回路最大邊
                        maxWeightInCycle(0,0,i,maxW,maxId);
                        
            if (dec > graph[0][i] - maxW)
                        
            {
                            dec 
            = graph[0][i] - maxW;
                            edgeId 
            = maxId;
                            id 
            = i;
                        }

                    }

                    
            if (dec >= 0)
                    
            {
                        
            break;
                    }

                    
            else
                    
            {
                        
            //將回路最大邊刪除,并修改為0點(diǎn)的邊
                        if (edgeId != -1)
                        
            {
                            edges[edgeId].from 
            = 0;
                            edges[edgeId].to 
            = id;
                            edges[edgeId].weight 
            = graph[0][id];
                        }

                        visited[id] 
            = true;
                        ans 
            += dec;
                    }

                }

                printf(
            "Total miles driven: %d\n",ans);
            }


            int main()
            {
                
            //freopen("data.txt","r",stdin);
                while(scanf("%d",&n) != EOF)
                
            {
                    Test();
                }

                
            return 0;
            }


            posted on 2011-06-02 11:49 bennycen 閱讀(1360) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久精品国产亚洲综合色| 久久中文字幕无码专区| 精品久久久久香蕉网| 9191精品国产免费久久| 综合久久精品色| 亚洲精品高清国产一久久| 伊人久久大香线蕉精品不卡 | 韩国三级中文字幕hd久久精品| 一本大道久久香蕉成人网| 日产精品99久久久久久| 国产成人香蕉久久久久| 久久久久久亚洲精品成人 | 久久久久久无码Av成人影院| 精品久久久久久无码人妻蜜桃| 午夜精品久久久久久久久| 久久99精品久久久久久齐齐| 精品久久久久久成人AV| 91麻豆国产精品91久久久| 国产精品成人无码久久久久久 | 无码精品久久一区二区三区| 久久99国产精品久久久| 亚洲女久久久噜噜噜熟女| 亚洲人成无码网站久久99热国产| 九九久久99综合一区二区| 久久亚洲精品国产精品| 欧美国产成人久久精品| 久久中文字幕视频、最近更新| 99久久国产综合精品成人影院| 国产精品久久久久jk制服| 亚洲中文字幕久久精品无码喷水| 久久se精品一区二区影院| 久久综合九色综合久99 | 色婷婷狠狠久久综合五月| 国产精品xxxx国产喷水亚洲国产精品无码久久一区| 亚洲AV日韩精品久久久久久久| 97香蕉久久夜色精品国产| 久久亚洲精品国产精品婷婷| 免费精品久久久久久中文字幕| 久久精品综合一区二区三区| 看全色黄大色大片免费久久久| 欧美激情精品久久久久久|