zoukankan      html  css  js  c++  java
  • USACO Section 2.4 Bessie Come Home (comehome)

    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
    
    思路:我们可以把a-z改成一个数组从1-26,同理A-Z改成27-52
    这样就可以floyd了
    注意这是个无向图!!!!
    Executing...
       Test 1: TEST OK [0.000 secs, 3236 KB]
       Test 2: TEST OK [0.000 secs, 3236 KB]
       Test 3: TEST OK [0.000 secs, 3236 KB]
       Test 4: TEST OK [0.000 secs, 3236 KB]
       Test 5: TEST OK [0.000 secs, 3236 KB]
       Test 6: TEST OK [0.000 secs, 3236 KB]
       Test 7: TEST OK [0.011 secs, 3236 KB]
       Test 8: TEST OK [0.000 secs, 3236 KB]
       Test 9: TEST OK [0.000 secs, 3236 KB]
    
    All tests OK.
    吐槽一句,那个f,bool数组,貌似根本就没用处啊!!!
     1 /*
     2 ID:wuhuaju2
     3 PROG:comehome
     4 LANG:C++
     5 */
     6 
     7 #include <cstdio>
     8 #include <iostream>
     9 #include <cstdlib>
    10 #include <algorithm>
    11 #include <cstring>
    12 using namespace std;
    13 
    14 int ans,x1,y1,dist,d[55][55],n;
    15 char x[2],y[2];
    16 bool f[55][55];
    17 
    18 void close()
    19 {
    20     fclose(stdin);
    21     fclose(stdout);
    22     exit(0);
    23 }
    24 
    25 void work()
    26 {
    27     for (int i=1;i<=52;i++)
    28         d[i][i]=0;
    29     for (int k=1;k<=52;k++)
    30        for (int i=1;i<52;i++)
    31            for (int j=1;j<=52;j++)
    32                 if (d[i][k]+d[k][j]<d[i][j])
    33                     d[i][j]=d[i][k]+d[k][j];
    34     char name;
    35     ans=100000000;
    36     for (int i=27;i<52;i++)
    37         if (d[i][52]<ans) 
    38         {
    39             name=char(i+38);
    40             ans=d[i][52];
    41         }
    42     cout<<name<<" "<<ans<<endl;
    43 }
    44 
    45 void init ()
    46 {
    47 freopen("comehome.in","r",stdin);
    48 freopen("comehome.out","w",stdout);
    49   scanf("%d",&n);
    50   for (int i=1;i<=52;i++)
    51       for (int j=1;j<=52;j++)
    52           d[i][j]=90000000;
    53   memset(f,false,sizeof(f));
    54   for (int i=1;i<=n;i++)
    55   {
    56       scanf("%s %s %d",x,y,&dist);
    57       if (x[0]>96)
    58       {
    59           x1=x[0]-96;
    60       } else x1=x[0]-38;
    61       if (y[0]>96)  //小写a-1,大写A-27
    62       {
    63           y1=y[0]-96;
    64       } else y1=y[0]-38;
    65       if (d[x1][y1]>dist)
    66       {
    67           d[x1][y1]=dist;
    68           d[y1][x1]=dist;
    69       }
    70       f[x1][y1]=true;
    71   }
    72 }
    73 
    74 int main ()
    75 {
    76     init();
    77     work();
    78     close();
    79     return 0;
    80 }
  • 相关阅读:
    编程小技巧之 Linux 文本处理命令(二)
    基金投资经验总结
    基金投资需要掌握的几个基本概念
    动目标指示MTI和动目标检测MTD
    雷达信号处理基础-快时间维和慢时间维
    信号处理第二篇——接着谈正弦信号
    毫米波雷达第一篇——测距篇
    信号处理第一篇——浅谈正弦信号
    python学习总结篇(3)——类
    python中的if __name__ == '__main__' 如何正确理解?
  • 原文地址:https://www.cnblogs.com/cssystem/p/2883848.html
Copyright © 2011-2022 走看看