• <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 2.4 Bessie Come Home

            Bessie Come Home

            Kolstad & Burch

            It's dinner time, and the cows are out in their separate pastures. Farmer John rings the bell so they will start walking to the barn. Your job is to figure out which one cow gets to the barn first (the supplied test data will always have exactly one fastest cow).

            Between milkings, each cow is located in her own pasture, though some pastures have no cows in them. Each pasture is connected by a path to one or more other pastures (potentially including itself). Sometimes, two (potentially self-same) pastures are connected by more than one path. One or more of the pastures has a path to the barn. Thus, all cows have a path to the barn and they always know the shortest path. Of course, cows can go either direction on a path and they all walk at the same speed.

            The pastures are labeled `a'..`z' and `A'..`Y'. One cow is in each pasture labeled with a capital letter. No cow is in a pasture labeled with a lower case letter. The barn's label is `Z'; no cows are in the barn, though.

            PROGRAM NAME: comehome

            INPUT FORMAT

            Line 1: Integer P (1 <= P <= 10000) the number of paths that interconnect the pastures (and the barn)
            Line 2..P+1: Space separated, two letters and an integer: the names of the interconnected pastures/barn and the distance between them (1 <= distance <= 1000)

            SAMPLE INPUT (file comehome.in)

            5
            A d 6
            B d 3
            C e 9
            d Z 8
            e Z 3
            

            OUTPUT FORMAT

            A single line containing two items: the capital letter name of the pasture of the cow that arrives first back at the barn, the length of the path followed by that cow.

            SAMPLE OUTPUT (file comehome.out)

            B 11
            Analysis

            Since the problem aims to solve a single-source shortest path problem, we can use the classical algorithms, such as Dijkstra, Bellman-Ford and the Floyd algorithm. Thanks to the small data amount, all of them are correct for this prblem.

            Code

            /*
            ID:braytay1
            TASK:comehome
            LANG:C++
            */

            #include 
            <iostream>
            #include 
            <fstream>
            using namespace std;
            ifstream fin(
            "comehome.in");
            ofstream fout(
            "comehome.out");

            int map[55][55];
            int dis[55];

            void bellman_ford(){
                dis[
            52]=0;
                
            for (int k=1;k<52;k++)
                    
            for (int u=1;u<=52;u++)
                        
            for (int v=1;v<=52;v++)
                            
            if ((dis[v]>dis[u]+map[u][v])&&map[u][v]<=10000
                                dis[v]
            =dis[u]+map[u][v];
            }

            char find_min(int a[55]){
                
            char source;
                
            int min=1000000;
                
            for (int i=27;i<=51;i++){
                    
            if (min>a[i]) {min=a[i];source=i-26+64;}
                }

                
            return source;
            }

            int main(){
                
            int N;
                fin
            >>N;
                memset(map,
            100,sizeof(map));
                
            for (int i=1;i<=N;i++){
                    
            char source,dest;
                    fin
            >>source>>dest;
                    
            int s,d;
                    s
            =(source>=97&&source<=122)?(source-96):(source-64+26);
                    d
            =(dest>=97&&dest<=122)?(dest-96):(dest-64+26);
                    
            int ds;
                    fin
            >>ds;
                    
            if (ds<map[s][d]) map[s][d]=ds;        
                    map[d][s]
            =map[s][d];
                }

                memset(dis,
            100,sizeof(dis));
                bellman_ford();
                
            char res;
                res
            =find_min(dis);
                fout
            <<res<<" "<<dis[res-64+26]<<endl;
                
            return 0;
            }

            posted on 2008-08-19 22:19 幻浪天空領主 閱讀(223) 評論(0)  編輯 收藏 引用 所屬分類: USACO

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

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案(2)

            文章分類(23)

            文章檔案(22)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            香蕉99久久国产综合精品宅男自 | 久久久91人妻无码精品蜜桃HD| 国产V综合V亚洲欧美久久| 久久精品aⅴ无码中文字字幕重口| 性高湖久久久久久久久| 久久久精品日本一区二区三区 | 久久久久无码中| 午夜欧美精品久久久久久久| 99久久综合狠狠综合久久| 伊人久久久AV老熟妇色| 精品人妻伦九区久久AAA片69 | 亚洲色大成网站www久久九 | 久久亚洲精品中文字幕| 国产精品99久久久久久猫咪 | …久久精品99久久香蕉国产| 久久精品国产亚洲AV不卡| 精品久久久久久久久午夜福利| 久久夜色精品国产亚洲av| 久久国产精品成人免费| 久久精品国产亚洲AV香蕉| 亚洲国产成人久久笫一页| 久久免费美女视频| 国产美女久久精品香蕉69| 一本久久精品一区二区| 久久久中文字幕日本| 精品99久久aaa一级毛片| 777久久精品一区二区三区无码| 久久综合给久久狠狠97色| 久久婷婷色综合一区二区| 亚洲午夜精品久久久久久app| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 久久亚洲2019中文字幕| 国内精品久久久久久久涩爱 | 久久精品国产亚洲av高清漫画| 伊人久久大香线蕉无码麻豆| 久久久久久青草大香综合精品 | 一本一本久久A久久综合精品 | 日日狠狠久久偷偷色综合0| 久久国产视频99电影| 日韩精品久久久久久久电影| 久久久久久久久久久|