• <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 幻浪天空領主 閱讀(260) 評論(0)  編輯 收藏 引用 所屬分類: USACO

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色诱久久av| 国产激情久久久久影院小草 | 亚洲伊人久久大香线蕉苏妲己| 久久精品国产2020| 久久青青草原亚洲av无码app| 一本久久综合亚洲鲁鲁五月天| 性高朝久久久久久久久久| 91精品国产高清久久久久久io| 91亚洲国产成人久久精品网址| 国产成人精品久久一区二区三区av| 无码任你躁久久久久久| 久久久久亚洲AV无码专区网站 | 久久综合视频网| 久久人爽人人爽人人片AV| 看全色黄大色大片免费久久久| 中文字幕久久久久人妻| 久久久久久亚洲精品无码| 国产精品女同久久久久电影院| 伊人精品久久久久7777| 亚洲嫩草影院久久精品| 91久久精一区二区三区大全| 性做久久久久久久久老女人| 久久99国产精品成人欧美| 久久久久久亚洲精品成人| 久久这里只精品99re66| 人妻无码久久精品| 久久青青草原精品国产不卡| 国产精品成人久久久久三级午夜电影 | 99久久www免费人成精品| 精品久久久久久国产潘金莲| 久久99久久99精品免视看动漫| 久久久久亚洲AV成人网| 国产精品美女久久久久av爽| 日本精品久久久中文字幕| 国产精品久久久久久久久免费| 无码国内精品久久人妻| 国产成人综合久久综合| 欧美va久久久噜噜噜久久| 国内精品久久久久久99| 久久精品国产亚洲网站| 色天使久久综合网天天|