• <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 - 106,  comments - 32,  trackbacks - 0
             

            Constructing Roads

            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
            Total Submission(s): 3780    Accepted Submission(s): 1298


            Problem Description
            There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

            We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
             

            Input
            The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

            Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

            Output
            You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.


            #include<iostream>
            #include
            <string.h>
            using namespace std;

            #define infinity 123456789
            #define max_vertexes 100 

            typedef 
            int Graph[max_vertexes][max_vertexes];

            Graph G;
            int total;
            int lowcost[max_vertexes],closeset[max_vertexes],used[max_vertexes];
            int father[max_vertexes];
            void prim(Graph G,int vcount)
            {
                
            int i,j,k;
                
            int min = infinity;
                
            for (i=0;i<vcount;i++)
                {
                    lowcost[i]
            =G[0][i];
                    closeset[i]
            =0
                    used[i]
            =0;
                    father[i]
            =-1
                }
                used[
            0]=1
                
                
            for (i=1;i<vcount;i++)
                {
                    j
            =0;
                    
                    
            while (used[j]) j++;
                    min 
            = lowcost[j];
                    
            for (k=0;k<vcount;k++)
                        
            if ((!used[k])&&(lowcost[k]<min)) 
                        {
                            min 
            =lowcost[k];
                            j
            =k;
                        }
                        father[j]
            =closeset[j]; 
                        used[j]
            =1;
                        total 
            += min;
                        
            for (k=0;k<vcount;k++)
                            
            if (!used[k]&&(G[j][k]<lowcost[k]))
                            { 
                                lowcost[k]
            =G[j][k];
                                closeset[k]
            =j; 
                            }
                }
            }

            int main()
            {
                
            int N,i,j,Q;
                
            int x,y;
                
            while(cin>>N)
                {
                    
                    total 
            = 0;
                    
            for(i =0; i< N;i++)
                    {
                        
            for(j = 0;j < N; j ++)
                            cin
            >>G[i][j];
                    }
                    cin
            >>Q;
                    
            for(i = 0; i < Q; i ++)
                    {
                        cin
            >>x>>y;
                        G[x
            -1][y-1= 0;
                        G[y
            -1][x-1= 0;
                    }
                    prim(G,N);
                    cout
            << total<<endl;
                }
                
            return 0;
            }


            posted on 2010-09-02 23:48 付翔 閱讀(301) 評論(0)  編輯 收藏 引用 所屬分類: ACM 圖論

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            CSDN - 我的blog地址

            博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产亚洲精品无码| 99精品久久精品一区二区| 国内精品人妻无码久久久影院| 久久99精品国产麻豆婷婷| 国产精品嫩草影院久久| 精品国产一区二区三区久久| 久久成人国产精品免费软件| 亚洲色欲久久久综合网| 精品久久久久中文字幕日本| 国产亚洲欧美成人久久片 | 无码人妻少妇久久中文字幕| 色偷偷88欧美精品久久久| 久久亚洲精品国产亚洲老地址| 亚洲中文精品久久久久久不卡| 久久精品国产亚洲AV嫖农村妇女| 久久亚洲国产午夜精品理论片| 国产精品美女久久久久av爽| 香蕉久久夜色精品国产尤物| 中文无码久久精品| 狼狼综合久久久久综合网| 亚洲AV无码久久精品色欲| 国产精品美女久久久久av爽 | 亚洲人AV永久一区二区三区久久 | 狠狠综合久久综合中文88| 日本WV一本一道久久香蕉| 热re99久久精品国产99热| 久久久中文字幕| 亚洲精品99久久久久中文字幕| 久久久老熟女一区二区三区| 国产三级精品久久| 久久亚洲私人国产精品vA| 色综合久久久久综合99| 精品熟女少妇aⅴ免费久久| 久久精品男人影院| 久久久久久免费一区二区三区 | 久久精品人人做人人爽电影蜜月| 亚洲欧美久久久久9999| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久午夜电影网| 久久夜色精品国产亚洲| 亚洲国产成人久久精品动漫|