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

            USACO Section 3.1 Agri-Net

            Agri-Net

            Russ Cox

            Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.

            Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.

            Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.

            The distance between any two farms will not exceed 100,000.

            PROGRAM NAME: agrinet

            INPUT FORMAT

            Line 1: The number of farms, N (3 <= N <= 100).
            Line 2..end: The subsequent lines contain the N x N connectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

            SAMPLE INPUT (file agrinet.in)

            4
            0 4 9 21
            4 0 8 17
            9 8 0 16
            21 17 16 0
            

            OUTPUT FORMAT

            The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

            SAMPLE OUTPUT (file agrinet.out)

            28
            Analysis

            A very traditional MST problem. Just use the Prim algorithm can get though it. Here I provide some simple descriptions about the Prim algorithm.
            The Prim algorithm plans to add a shortest path to the set A, which records the MST, and abandon the edge composed a cycle.

            Code

            /*
            ID:braytay1
            PROG:agrinet
            LANG:C++
            */

            #include 
            <iostream>
            #include 
            <fstream>
            using namespace std;
                
            int N;
            int map[101][101];
            int pi[101],key[101];
            bool Q[101];
            int min(int *a,bool *b){
                
            int res=10000000,mini;
                
            for (int i=0;i<N;i++){
                    
            if (res>*(a+i)&&*(b+i)==false{res=*(a+i);mini=i;}
                }

                
            return mini;
            }

            bool isempty(bool *a){
                
            for (int i=0;i<N;i++){
                    
            if (!*(a+i)) return false;
                }

                
            return true;
            }

            void prim(int g[101][101],int r){
                
            for (int u=0;u<N;u++){
                    key[u]
            =1000000;
                    pi[u]
            =0;
                    Q[u]
            =false;
                }

                key[r]
            =0;
                
            int u1;
                
            while (!isempty(Q)){
                    u1
            =min(key,Q);
                    Q[u1]
            =true;
                    
            for (int v=0;v<N;v++){
                        
            if (g[u1][v]&&Q[v]==false&&g[u1][v]<key[v]) {pi[v]=u1;key[v]=g[u1][v];}
                    }
                    
                }

            }

            int main(){
                ifstream fin(
            "agrinet.in");
                ofstream fout(
            "agrinet.out");
                fin
            >>N;
                
            for (int i=0;i<N;i++)
                    
            for (int j=0;j<N;j++){
                        fin
            >>map[i][j];
                    }

                prim(map,
            0);
                
            int res=0;
                
            for (int i=0;i<N;i++) res+=key[i];
                fout
            <<res<<endl;
                
            return 0;
            }


             

            posted on 2008-08-20 15:05 幻浪天空領(lǐng)主 閱讀(256) 評(píng)論(0)  編輯 收藏 引用 所屬分類: USACO

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            三上悠亚久久精品| 精品久久人人爽天天玩人人妻| 日本五月天婷久久网站| 精品国产乱码久久久久软件| 色欲av伊人久久大香线蕉影院| a级成人毛片久久| 久久人妻少妇嫩草AV无码蜜桃| 亚洲av伊人久久综合密臀性色| 国产999精品久久久久久| 大香伊人久久精品一区二区| 久久久老熟女一区二区三区| 麻豆国内精品久久久久久| 久久精品亚洲AV久久久无码| 91久久香蕉国产熟女线看| 一本久久a久久精品亚洲| 久久se精品一区精品二区国产| 亚洲午夜久久久影院| 国内精品久久久久久久涩爱| 久久精品中文字幕无码绿巨人| 久久精品无码一区二区app| AV狠狠色丁香婷婷综合久久| 久久精品中文无码资源站| 久久99精品国产麻豆婷婷| 嫩草影院久久99| 欧美噜噜久久久XXX| 奇米影视7777久久精品人人爽| 久久99精品国产99久久6| 久久精品国产亚洲综合色| 成人久久免费网站| 四虎影视久久久免费观看| 女人香蕉久久**毛片精品| 久久777国产线看观看精品| 精品国产乱码久久久久久1区2区| 久久亚洲熟女cc98cm| 久久综合亚洲色HEZYO国产| 久久久久国色AV免费观看| 一本久久a久久精品综合夜夜 | 久久久久久久综合狠狠综合| 99久久成人18免费网站| 99久久国产亚洲高清观看2024| 国产高潮久久免费观看|