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


            久久精品一本到99热免费| 久久精品中文无码资源站| 99久久精品国产综合一区| 99久久久久| 久久人人爽人人人人片av| 99久久无色码中文字幕| 久久久久九国产精品| 亚洲综合熟女久久久30p| 国产精品久久国产精品99盘| 久久久久无码精品| 麻豆AV一区二区三区久久| 伊人热人久久中文字幕| 99久久精品免费看国产一区二区三区| 久久久久久九九99精品| 久久av高潮av无码av喷吹| 久久99精品久久只有精品| 青青热久久国产久精品| 2021久久国自产拍精品| 午夜精品久久久久| 久久精品国产99久久久香蕉| 久久免费的精品国产V∧| 中文精品久久久久人妻| 欧美精品福利视频一区二区三区久久久精品 | 国产精品va久久久久久久| 国内精品久久久久伊人av| 久久久久久无码国产精品中文字幕 | 国产午夜精品理论片久久影视| 日日狠狠久久偷偷色综合免费| 香蕉久久夜色精品国产小说| 日日躁夜夜躁狠狠久久AV| 中文国产成人精品久久不卡| 中文精品99久久国产| 欧美激情精品久久久久久久九九九| 91精品国产综合久久四虎久久无码一级| 无码日韩人妻精品久久蜜桃| 久久精品国产亚洲AV香蕉| 国产精品狼人久久久久影院| 国产精品天天影视久久综合网| 久久无码人妻一区二区三区| 无码国产69精品久久久久网站| 日韩AV无码久久一区二区|