• 2.4.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
    
    /*
    ID: makeeca1
    LANG: C++
    TASK: comehome
    */
     
    #include <algorithm>
    #include <climits>
    #include <cstdio>
    #include <cctype>
    #include <queue>
    using namespace std;
    bool flag[52] = {};
    int a[52][52], dist[52];
     
    int main()
    {
        int p;
        freopen("comehome.in", "r", stdin);
        freopen("comehome.out", "w", stdout);
        for (int i = 0; i < 52; ++i)
            fill_n(a[i], 52, INT_MAX/2);
        for (scanf("%d", &p); p; --p)
        {
            char u, v;
            int w;
            scanf("%*c%c %c%d", &u, &v, &w);
            u = islower(u) ? u-'a' : u-'A'+26;
            v = islower(v) ? v-'a' : v-'A'+26;
            if (w < a[u][v])
                a[u][v] = w, a[v][u] = w;
        }
        copy(a[51], a[51]+52, dist);
        for(;;)
        {
            int min_dist = INT_MAX, u;
            for (int i = 0; i < 51; ++i)
                if (!flag[i] && dist[i] < min_dist)
                    min_dist = dist[i], u = i;
            if (u >= 26) return printf("%c %d
    ", u-26+'A', min_dist), 0;
            flag[u] = true;
            for (int i = 0; i < 51; ++i)
                if (!flag[i] && dist[u]+a[u][i] < dist[i])
                    dist[i] = dist[u]+a[u][i];
        }
    }
  • 相关阅读:
    10. 正则表达式匹配(动态规划)
    8. 字符串转换整数 (atoi)
    5. 最长回文子串
    4. 寻找两个正序数组的中位数
    1109. 航班预订统计
    计算机网络面试整理
    HTTP 响应 代码
    Tomcat 安装 2019.1.20
    ubuntu 编译 jdk (三)
    ubuntu 编译 jdk (二)
  • 原文地址:https://www.cnblogs.com/makeecat/p/3274638.html
Copyright © 2020-2023  润新知